Skip to main content

Threads in Java - Thread Pools (Part 10)

Server applications are one of the most common use cases in Java. One aspect of server applications is to handle incoming requests and process them. This seems simple to implement but it has its own disadvantages.

Since we are creating a new thread for each incoming request, the system would consume more time and resources in creating and destroying threads. This may also lead to the infamous OutOfMemoryError.

To deal with such problems, Java provides the concept of Thread Pools. Let us understand this in detail in the below section.

Thread Pools

When we need to limit the number of threads running in our application, the thread pool proves helpful. There is a performance overhead in starting a new thread and each thread is allocation some stack memory. 

Instead of starting a new thread for executing a task concurrently, we can pass the task to a thread pool. As soon as the thread pool has any idle thread, the task is assigned to it and the thread executes the desired task.

Thread Pool implements a Blocking Queue internally in which the tasks are inserted initially. The idle threads in the pool deque these tasks one by one to execute. The rest of the idle threads in the queue will be blocked waiting to deque tasks. When any thread completes its execution, it can pick up a new task to execute. When all tasks are completed, the threads remain active and wait for more tasks in the queue.

Thread Pool Executor

Since Java 5, we have the Executor framework. This revolves around the Executor interface, its subinterface ExecutorService and ThreadPoolExecutor class which implements both interfaces. 

ThreadPoolExecutor separates the task creation and its execution. Using this, we only have to implement Runnable objects and send them to the executor. The executor is responsible for their creation, execution, instantiation etc. 

How to create ThreadPoolExecutor

We can create it in five ways - 

  • Fixed thread pool executor - This creates a thread pool executor with a fixed number of threads. These threads can execute any number of tasks. If all threads are active and additional tasks are submitted, then they have to wait in the queue until a thread is available.
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
  • Cached thread pool executor - This can create new threads as needed but will reuse previously created threads when they are idle. We should use this very carefully as in long-running tasks it can bring the system down.
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
  • Scheduled thread pool executor - This can schedule the tasks to run periodically.
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(10);
  • Single thread pool executor - Creates only one thread to execute tasks.
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newSingleThreadExecutor();
  • Work stealing thread pool executor - Creates a thread pool that maintains enough threads to support the given parallelism level. Here parallelism level means the maximum number of threads which will be used to execute a given task, at a single point of time, in multi-processor machines.
    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newWorkStealingPool(4);

Code Examples

Below are code examples to see concepts in action - 

  • ExecutorService example
  • The output of this program will be -
    Finished all threads
    pool-1-thread-1 - Start. Command - 0
    Command is processing...
    pool-1-thread-2 - Start. Command - 1
    Command is processing...
    pool-1-thread-3 - Start. Command - 2
    Command is processing...
    pool-1-thread-3 End.
    pool-1-thread-3 - Start. Command - 3
    Command is processing...
    pool-1-thread-1 End.
    pool-1-thread-2 End.
    pool-1-thread-1 - Start. Command - 4
    Command is processing...
    pool-1-thread-3 End.
    pool-1-thread-1 End.
  • ThreadPoolExecutor example
  • The output of this program will be -
    Created: Task - 0
    Created: Task - 1
    Created: Task - 2
    Created: Task - 3
    Created: Task - 4
    Executing: Task - 0
    Executing: Task - 1
    Executing: Task - 2
    Executing: Task - 3
    Executing: Task - 4


Congratulations!! 🙋 today we discussed the concepts of Thread Pools, Executor Framework with their implementations. I hope you enjoyed this post.

You can find the complete code of this project on my GitHub.Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like have suggestions from you to make it better. 

Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding 😊


Popular posts from this blog

Parsing XML using Retrofit

Developing our own type-safe HTTP library to interface with a REST API can be a real pain as we have to handle many aspects - making connections caching retrying failed requests threading response parsing error handling, and more.  Retrofit, on the other hand, is a well-planned, documented and tested library that will save you a lot of precious time and headaches. In this tutorial, we are going to discuss how we can parse the XML response returned from  using the Retrofit library. To work with Retrofit, we need three classes -  Model class to map the JSON data Interfaces which defines the possible HTTP operations Retrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL endpoint for the HTTP operation. Every method of the above interface represents on possible API call. The request type is specified by using appropriate annotations (GET, POST). The respon

Threads in Java - CountDownLatch (Part 12)

A CountDownLatch is a synchronizer which allows one thread to wait for one or more threads before starts processing. A good application of  CountDownLatch is in Java server-side applications where a thread cannot start execution before all the required services are started. Working A  CountDownLatch is initialized with a given count which is the number of threads it should wait for. This count is decremented by calling countDown() method by the threads once they are finished execution. As soon as the count reaches to zero, the waiting task starts running. Code Example Let us say we require three services, LoginService, DatabaseService and CloudService to be started and ready before the application can start handling requests. Output Cloud Service is up! Login Service is up! Database Service is up! All services are up. Now the waiting thread can start execution. Here, we can see that the main thread is waiting for all the three services to start before starting its own

Threads in Java - Masterclass (Part 0)

Threads in Java Multithreading is a way to introduce concurrency in a program. In any case, if there are parallel paths in our program (parts which do not depend on the result from another part), we can make use of multithreading. One should exploit this feature, especially with all these multiple core machines nowadays. Below are a few reasons why we should use multithreading - 1. Keep a process responsive There was once a time when you would print a document in MS Word and the application would freeze for an annoyingly long amount of time until the job finished. Eventually, Microsoft solved this problem by running a printing job parallel to the main thread/ GUI thread.  To be clear though, not only GUI apps but Network services have to keep an ear to the ground for new clients, dropped connections and cancellation requests. In either case, it is critical to do the heavy lifting on a secondary thread to keep the user satisfied. 2. Keep a processor busy Keeping a proc