The thread pool in .NET

CLR provides a separate thread pool that contains the list of threads to be used to execute tasks asynchronously. Each process has its own specific thread pool. CLR adds and removes threads in or from the thread pool.

To run a thread using ThreadPool, we can use ThreadPool.QueueUserWorkItem, as shown in the following code:

class Program 
{ 
  static void Main(string[] args) 
  { 
    ThreadPool.QueueUserWorkItem(ExecuteLongRunningOperation, 1000); 
    Console.Read(); 
  } 
  static void ExecuteLongRunningOperation(object milliseconds) 
  { 
 
    Thread.Sleep((int)milliseconds); 
    Console.WriteLine("Thread is executed"); 
  } 
} 

QueueUserWorkItem queues the task to be executed by the CLR in a thread that is available in the thread pool. The task queues are maintained in First In, First Out (FIFO) order. However, depending on the thread's availability and the task job itself, the task completion may be delayed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.116.126