In my previous post I told you about myself and about the expectations from the blog. Today, I am going to write first technical post about Thread Pool.
Post will contain the following things about Thread pool:
- Why to use it?
- When to use it?
- Pros and cons
A Thread pool is a pattern to achieve the concurrency of execution in a computer program. A thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program.
By maintaining a pool of threads, the model increases performance and avoids latency in execution due to frequent creation and destruction of threads for short-lived tasks.
Why to use Thread Pool?
This is the main question which everyone can raise that why to use Thread Pool if there is already a concept to achieve concurrency which is initializing new threads each time and perform some task in each thread and destroy them whenever required.
Since a thread can only run once, you’d have to use a thread per task. However, creating and starting threads is somewhat expensive and can lead to a situation where too many threads are waiting for the executions and it can lead to overhead of the memory which can reduce the performance of the program. Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead because switching between threads perform following things internally:
- The thread is interrupted, the context of the CPU switched from user mode to kernel mode.
- CPU context is saved, the CPU context for the new thread to be scheduled is loaded.
- Then the context is switched from kernel mode to user mode.
This is quite a lot of work. When you have multiple threads starving for attention, it’s very hard to optimize the scheduler for such a situation, and to schedule fairly.
On the other hand, a thread pool is a pool of working threads which are always running. There is no need to initialize and destroy threads again and again. They just pickup from the pool and perform their task and if there is no task then thread will wait. So thread pool minimizes the overhead due to thread creation.
Pros of using Thread Pool:
- No need to create new thread per task so it will save the work which is done when a new thread is initialized.
- It helps in tracking the status of thread, exception handling, task cancellation, progress reporting of the task.
Cons of using Thread Pool:
- Thread Pools threads are not suitable for long running operation because it can lead to thread starvation.
When to use Thread Pool?
Thread Pools are useful only in a Server-client kind of situation where the number/occurrence of client requests cannot be determined/predicted. In this scenario, creating a new Thread each time a client request is made has two dis-advantages:
- Run time latency for thread creation: As creation of a thread requires some time, thus the actual job doesn’t start as soon as client make a request there can by a delay.
- Uncontrolled use of System resources: Thread consumes system resources, thus the system may run out of the resources.
Thread Pool addresses aforementioned problems by creating specified number of threads on server start-up instead of creating them during the run time and it limits the number of threads that are running at any given time.
In my next post will cover about how to create and use thread pool in java.