I am again here to continue the discussion around Threading. Today we will discuss about Thread Pool and related concepts.
In case, you didn’t have a look at our previous posts, you can go through the following,
- Threading Simplified: Part 1
- Threading Simplified: Part 2
- Threading Simplified: Part 3
- Threading Simplified: Part 4
Let’s begin the discussion by putting questions as in the following,
What is Thread Pool
Thread pool is a collection or queuing of threads that can be used to execute various tasks in the background. It enforces notion of reusability instead of creating new threads all the time.
Once any thread is done with the assigned task, it comes back in the queue of waiting threads to be reused for some other task.
Why we need it
Thread pool executes the task in background and it helps the main thread to avoid blocking or waiting for long running tasks.
When to use it?
Typically it is used when we want to execute many short lived tasks. One example could be the server application when we have batch processing involved.
How we can use it?
There are several ways to use them as in the following:
- Using QueueUserWorkItem method.
- Using Asynchronous delegates.
- Using BackgroundWorker [Will discuss in upcoming articles].
- Using TPL [Task Parallel Library] [Will discuss in upcoming articles].
Using ThreadPool.QueueUserWorkItem method:
Let’s see how to create pooled threads via QueueUserWorkItem.
- static void Main(string[] args)
- {
- Console.Title = "Thread Pool Demo";
- ThreadPool.QueueUserWorkItem(DoWork);
- ThreadPool.QueueUserWorkItem(DoWork, "ABC"); //With parameter
- }
- static void DoWork(object data)
- {
- Console.WriteLine("I am a pooled thread using QueueUserWorkItem. My value: {0}", data);
- }

Please note that the callback method needs to have a parameter of object type, even if you don’t pass it while calling. This is required due to delegate signature.
Using Asynchronous delegates
Asynchronous delegate solves the few constrains or issues which are there with ThreadPool.QueueUserWorkItem method as in the following,
- There is no easy way to get return value from callback method when using QueueUserWorkItem. Asynchronous delegates addresses this constraint.
- Unhandled exceptions in the case of QueueUserWorkItem, needs to be handled explicitly in callback method whereas with Asynchronous delegates, these exceptions can be easily re-thrown to the original thread or the thread which calls EndInvoke.
Now let’s have a look how to create pooled threads via Asynchronous delegates.
- static void Main(string[] args)
- {
- Console.Title = "Thread Pool Demo";
- Func < string, int > MethodName = GetStringLength;
- IAsyncResult result = MethodName.BeginInvoke("TestAsyncDelegates", null, null);
- //Other code which can run in parallel
- int stringLength = MethodName.EndInvoke(result);
- Console.WriteLine("string length is: {0}", stringLength);
- }
- static int GetStringLength(string inputString)
- {
- Console.WriteLine("I am a pooled thread using AsynchronousDelegates. My value: {0}", inputString);
- return inputString.Length;
- }
Output

As you can see in the above screenshot that first message printed using callback method GetStringLength and the second message printed in the calling thread with the value returned from callback method.
Also please note that pooled thread are always background. You can test this by putting the following code in the callback method.
- Console.WriteLine("Is background thread? {0}", Thread.CurrentThread.IsBackground);
- Console.WriteLine("Is polled thread? {0}", Thread.CurrentThread.IsThreadPoolThread);

Hope you have liked the article. Looking forward for your comments/suggestions.

Prakash TripathiPosted Mar 6, 2016, 11:12 PM
Thnx Sr Karthiga.
Prakash TripathiPosted Mar 6, 2016, 11:12 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 11:12 PM
Thnx Udaybhan.
Prakash TripathiPosted Mar 6, 2016, 11:12 PM
Thnx Raja.
Prakash TripathiPosted Mar 6, 2016, 11:11 PM
Thnx Santhakumar.
Prakash TripathiPosted Mar 6, 2016, 11:11 PM
Thnx Sridhar.
Prakash TripathiPosted Mar 6, 2016, 11:11 PM
Thnx Humayun.
Kumaresh RajalingamPosted Mar 6, 2016, 8:31 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:30 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:21 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:21 PM
Nice explanation
Uday Bhan SinghPosted Dec 21, 2015, 10:31 PM
Nice, Thanks for Sharing .I like it
Raja TPosted Dec 7, 2015, 7:16 AM
Nice, thanks for sharing
Santhakumar MunuswamyPosted Dec 7, 2015, 6:51 AM
Good One
Sridhar SharmaPosted Dec 7, 2015, 5:32 AM
Nice share :)
Humayun Kabir MamunPosted Dec 7, 2015, 4:26 AM
Nice...