Thread Pool

Hello Readers,

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:

  1. Introduction
  2. Why to use it?
  3. When to use it?
  4. Pros and cons

Let’s start!


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:

  1. The thread is interrupted, the context of the CPU switched from user mode to kernel mode.
  2. CPU context is saved, the CPU context for the new thread to be scheduled is loaded.
  3. 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:

  1. No need to create new thread per task so it will save the work which is done when a new thread is initialized.
  2. It helps in tracking the status of thread, exception handling, task cancellation, progress reporting of the task.

Cons of using Thread Pool:

  1. 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:

  1. 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.
  2. 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.


One thought on “Thread Pool

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s