Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Thread.Sleep affects creation of new Tasks?

private static void Main(string[] args)
{
    for (int i = 0; i < 1000; i++)
    {
        Task.Factory.StartNew(() =>
        {
            Thread.Sleep(1000);
            Console.WriteLine("hej");
            Thread.Sleep(10000);
        });    
    }
    Console.ReadLine();
}

Why this code won't print 1000 times "hej" after one second? Why Thread.Sleep(10000) has an impact on code behavior?

like image 268
MistyK Avatar asked Oct 01 '14 12:10

MistyK


People also ask

Does thread sleep create a new thread?

Java Thread Sleep important points For a quiet system, the actual time for sleep is near to the specified sleep time but for a busy system it will be little bit more. Thread sleep doesn't lose any monitors or locks current thread has acquired.

Why we should not use thread sleep?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

What happens when a thread sleeps?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Does thread sleep block the thread?

Sleep method. Calling the Thread. Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread. Once that interval elapses, the sleeping thread resumes execution.


Video Answer


2 Answers

Factory.StartNew effectively delegates the work to ThreadPool.

Threadpool will create number of threads immediately to respond the request as long as threads count is less than or equal to processor count. Once it reaches processor count, threadpool will stop creating new threads immediately. That makes sense, because creating number of threads more than processor count introduces Thread scheduling overhead and returns nothing.

Instead it will throttle the creation of threads. It waits for 500 ms to see if any work still pending and no threads to process the request. If pending works are there, it will introduce a new thread(only one). This process keeps on going as long as you have enough works to do.

When work queue's traffic is cleared, threadpool will destroy the threads. And above mentioned process keeps on going.

Also, There is a max limit for number of threads threadpool can run simultaneously. If you hit that, threadpool will stop creating more threads and wait for previous work items to complete, So that it can reuse the existing thread.

That's not the end of story, It is convoluted! These are few decisions taken by ThreadPool.

I hope now that will be clear why you see what you see.

like image 142
Sriram Sakthivel Avatar answered Sep 30 '22 20:09

Sriram Sakthivel


There are a multitude of factors that would alter the result.

Some being (but not limited to):

  • The inherent time for the iteration of the loop
  • The size of the thread pool
  • Thread management overhead
like image 29
David Pilkington Avatar answered Sep 30 '22 20:09

David Pilkington