Designing a Thread Pool Framework Part 1: What’s the need of a Thread Pool
In this we will discuss what is a Thread pool and why do we need Thread Pools.
Many times you encounter a situation where where you need to perform several tasks in parallel to improve the performance of application.
These Tasks can be anything like,
- Encrypting a file.
- Calculating hash of a file.
- Performing some heavy calculation.
First default solution that comes in our mind is lets create a separate thread for each Task.
But what a minute!!!!
What if we have 3000 Tasks to do in parallel?
With this solution we are going to need 3000 Threads. Now that’s bad and also its not possible in a 32 bit application, because each thread has a stack size associated with it so creating 3000 threads at a time will make our application to crash.
Now lets think of an another feasible solution.
What if we create a specific number of threads and ask each thread to perform few tasks in sequence. Like, if we create 50 threads and each thread executes 60 Tasks in sequence. But all these 50 Threads will run in parallel and execute their 60 Tasks in parallel. Obviously performance will not be as expected as executing each Task separately but still it will be much much better than executing 3000 tasks in parallel and crashing your application .
For example, if each Task takes 1 second to complete,
- Executing 3000 Tasks sequentially will take 3000 seconds i.e. 5 Minutes
- Executing 3000 Tasks with a Thread Pool of 50 Threads will take 60 seconds i.e. 1 Minute.
Now that’s called a Thread Pool.
A thread pool is a group of threads which remains ready to perform tasks. Each thread in a Thread Pool performs several Tasks in sequence. So, instead of creating a new Thread for every Task, a group of specific number of Threads are used to improve the performance and make things scalable.
An important Point about Thread Pools
While designing a Thread Pool we should be careful while choosing the number of threads, because if number of Threads in a Thread Pool are more or less than required then it can hamper the process’s performance and stability. Therefore, we should design a Thread Pool in such a way that number of threads in it should be configurable at run time based on current situation of Load.
In the next part of this series we will see how to design a Thread Pool from scratch.