SIGN UP MEMBER LOGIN:    
ARTICLE

ThreadPool class in .NET

Posted by Ashish Shukla Articles | C# Language April 18, 2011
This article describes the use of the ThreadPool class in .NET.
Reader Level:

ThreadPooling

Multithreading is used to perform some tasks in the background typically so that the main application thread or UI thread is not blocked. But there is an overhead involved to create new threads and if the operations performed by these threads are quite simple then the creation and destruction of new threads can have impact on the performance of the system.

The ThreadPool class in the System.Threading namespace can be used if we are required to create large number of threads. ThreadPool is a collection of threads which can be used and returned to the pool once the processing is complete.

Following diagram illustrates the how threads are used from the ThreadPool :

ThreadPool1.gif

In the above diagram there are three requests in the Requests Queue and two threads in the thread pool, in such a scenario the extra request waits its turn in the requests queue till the thread becomes available to process it.

As soon as we create a new thread pool it creates a new thread to serve the requests and keeps on creating the new threads unitl it reaches a defined limit.

And after the thread completes its job it is not destroyed immediately instead it waits for some duration for the new request before it gets destroyed.

When a thread from the thread pool is reused the data in the thread local storage remains. Also the fields that are marked with the ThreadStaticAttribute retains there data.

But the threads in the ThreadPool are all background threads so when the foreground thread exits the
Thread also terminates.

To execute the method in the thread from the threadpool we need to pass the QueueUserWorkItem a
Waitcallback delegate instance to which we need to pass the method we have to call.The method should be marked as static and void.

class Program
    {

        static void Main(string[] args)
        {
          ThreadPool.QueueUserWorkItem (new WaitCallback(Process),null);
         }

         public static void Process(object obj)
        {
            Console.WriteLine("Background thread started ");
        }


As you see in the above code we pass null as the second parameter, in place of null we can pass the information to the method.

To check the number of threads currently available in the thread pool we can use the GetAvailableThreads method which has the following signature:

public static void GetAvailableThreads(out int workerThreads, out int completionPortThreads);

Following gets the number of available worker and IO threads.

int availableWorkers = 0;
int availableAsyncIO = 0;
ThreadPool.GetMaxThreads(out availableWorkers, out availableAsyncIO);

Once a workitem has been queued in the pool it can not be cancelled.There can be one thread pool per process.
 

Login to add your contents and source code to this article
share this article :
post comment
 

hi im lem, and im a programmer here in phil, i want to learn more about pooling and threading. ill start here in your article^^ thanks..maybe sometime you can teach me more^^

Posted by lem pascua Sep 20, 2011

hi im lem, and im a programmer here in phil, i want to learn more about pooling and threading. ill start here in your article^^ thanks..maybe sometime you can teach me more^^

Posted by lem pascua Sep 20, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor