There are a lot of discussions on the pros and cons of using the ThreadPool and creating your own threads. Having spent a bit of time reading what others have to say, here’s a summary of the things I’ve picked up on.
The problem with creating your own threads
Creating and destroying threads has a high CPU usage, so when you need to perform lots of small, simple tasks concurrently the overhead of creating your own threads can take up a significant portion of the CPU cycles and severely affect the final response time. This is especially true in stress conditions where executing multiple threads can push CPU to 100% and most of the time would be wasted in context switching (swapping threads in and out of the processor along with their memory).
Using the Thread Pool
This is where the .Net Thread Pool comes in, where a number of threads are created ahead of time and kept around to pick up any work items you give them to do, without the overhead associated with creating your own threads.
When not to use the Thread Pool
In an ideal world you would always want to use the Thread Pool, but there are some real-world limitations. Most importantly, and the reason why most experts would tell you not to use the Thread Pool except for brief jobs is that: there is a limited number of threads in the .Net Thread Pool (250 per CPU by default), and they are being used by many of the .Net framework classes (e.g. timer events are fired on thread pool threads) so you wouldn’t want your application to hog the thread pool.
There are also a number of situations where you shouldn’t use the thread pool:
- You require a foreground thread, all the thread pool threads are background threads
- You require a thread to have a particular priority.
- You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting.
- You need to place threads into a single-threaded apartment. All ThreadPool threads are in the multithreaded apartment.
- You need to have a stable identity associated with the thread, or to dedicate a thread to a task.
Exceptions in Thread Pool threads
Unhandled exceptions on thread pool threads terminate the process with 3 exceptions:
- A ThreadAbortException is thrown in a thread pool thread, because Abort was called.
- An AppDomainUnloadedException is thrown in a thread pool thread, because the application domain is being unloaded.
- The common language runtime (CLR) or a host process terminates the thread.
When to create your own threads
As I’ve mentioned already, creating your own threads is bad when lots of simple tasks require a relative large overhead in context switching, and the Thread Pool is bad for long running, or blocking tasks. Which leads to the natural conclusion :-P – create your own threads for long running, or blocking tasks!
When working with the Thread Pool there are some useful methods at your disposable, including:
- GetAvailableThreads method which returns the number of threads available to you
- GetMinThreads method returns the number of idle threads the thread pool maintains in anticipation of new requests
- GetMaxThreads method returns the max number of thread pool threads that can be active concurrently
- SetMinThreads method sets the number of idle threads the thread pool maintains in anticipation of new requests
- SetMaxThreads method sets the number of thread pool threads that can be active concurrently
If you’re interested in how the ThreadPool class dynamically manages the size of the thread pool under the hood (despite giving you the option to set min and max threads) you should have a read of Pedram Razai’s blog post in the reference section.
And before you go, I mentioned earlier that all Thread Pool threads are background threads, so how do they differ from foreground threads? Well, foreground and Background threads are identical with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong