Tuesday, July 23, 2013

How to create Thread Pools using Java 1.5 Executor Framework - Example Tutorial

Java 1.5 introduced Thread pool in Java in the form of Executor framework, which allows Java programmer to decouple submission of a task to execution of the task. If you are doing server side programming in Java than Thread pool is an important concept to maintain scalability, robustness, and stability of the system. For those, who are not familiar with thread pool in Java or concept of thread pool here is one liner, Thread pool in Java is a pool of worker threads, which is ready to perform any task given to them, mostly in the form of implementation of Runnable or Callable interface. Since Java supports multithreading in programming language itself, it allows multiple threads to run concurrently and perform parallel processing of the task. In this article, we will learn following things about thread pool in Java :
  1. What is Thread pool in Java?
  2. Why do we need Thread pool in Java ?
  3. What is Executor framework in Java 5?
  4. How to create fixed size thread pool using Executor framework in Java?
  5. Benefits of using Thread Pool in Java?

What is Thread Pool in Java and why we need it

As I said Thread pool is a pool of already created worker thread ready to do the job. The thread pool is one of essential facility any multi-threaded server side Java application requires. One example of using thread pool is creating a web server, which processes client request. If you are familiar with socket programming then you know that ServerSocket.accept() is blocking method and blocks until a socket connection made. 

If only one thread is used to process client request, than it subsequently limit how many client can access server concurrently. In order to support large number of clients, you may decide to use one thread per request paradigm, in which each request is processed by separate Thread, but this require Thread to be created, when request arrived.  Since creation of Thread is time consuming process, it delays request processing. 

It also limits number of clients based upon how many thread per JVM is allowed, which is obviously a limited number. Thread pool solves this problem for you, It creates Thread and manage them. Instead of creating Thread and discarding them once task is done, thread-pool reuses threads in form of worker thread. 

Since Thread are usually created and pooled when application starts, your server can immediately start request processing, which can further improve server’s response time. Apart from this, there are several other benefits of using Thread pool in Java applications, which we will see in subsequent section. 

In short, we need thread pools to better mange threads and decoupling task submission from execution. Thread pool and Executor framework introduced in Java 5 is an excellent thread pool provided by library.

Java Thread Pool - Executor Framework in Java 5

Java 5 introduced several useful features like Enum, Generics, Variable arguments and several concurrency collections and utilities like ConcurrentHashMap and BlockingQueue etc, It also introduced a full feature built-in Thread Pool framework commonly known as Executor framework

The core of this thread pool framework is Executor interface which defines an abstraction of task execution with method execute(Runnable task) and ExecutorService which extends Executor to add various life-cycle and thread pool management facilities like shutting down thread pool. 

Executor framework also provides a static utility class called Executors ( similar to Collections) which provides several static factory method to create various type of Thread Pool implementation in Java e.g. fixed size thread pool, cached thread pool and scheduled thread pool. Runnable and Callable interface are used to represent task executed by worker thread managed in these Thread pools. 

Interesting point about Executor framework is that, it is based on Producer consumer design pattern, where application thread produces task and worker thread consumers or execute those task, So it also suffers with limitation of Producer consumer task like if production speed is substantially higher than consumption than you may run OutOfMemory because of queued task, of course only if your queue is unbounded.
Java Thread Pool Example Executor Framework

How to create fixed size thread pool using Executor framework in Java?

Creating fixed size thread pool using Java 5 Executor framework is pretty easy because of static factory methods provided by Executors class. All you need to do is define your task which you want to execute concurrently and than submit that task to ExecutorService. from them Thread pool will take care of how to execute that task, it can be executed by any free worker thread and if you are interested in result you can query Future object returned by submit() method. Executor framework also provides different kind of Thread Pool e.g. SingleThreadExecutor which creates just one worker thread or CachedThreadPool which creates worker threads as and when necessary. You can also check  Java documentation of Executor Framework for complete details of services provided by this API. Java concurrency in Practice also has a couple of chapters dedicated to the effective use of Java 5 Executor framework, which is worth reading for any senior Java developer.

Example of Thread Pool in Java

Here is an example of Thread pool in Java, which uses Executor framework of Java 5 to create a fixed thread pool with a number of worker thread as 10. It will then create task and submit that to Thread pool for execution:

public class ThreadPoolExample {

    public static void main(String args[]) {
       ExecutorService service = Executors.newFixedThreadPool(10);
       for (int i =0; i<100; i++){
           service.submit(new Task(i));

final class Task implements Runnable{
    private int taskId;
    public Task(int id){
        this.taskId = id;
    public void run() {
        System.out.println("Task ID : " + this.taskId +" performed by " 
                           + Thread.currentThread().getName());

Task ID : 0 performed by pool-1-thread-1
Task ID : 3 performed by pool-1-thread-4
Task ID : 2 performed by pool-1-thread-3
Task ID : 1 performed by pool-1-thread-2
Task ID : 5 performed by pool-1-thread-6
Task ID : 4 performed by pool-1-thread-5

If you look at the output of this Java example you will find different threads from thread pool are executing tasks.

Benefits of Thread Pool in Java

Thread Pool offers several benefit to Java application, biggest of them is separating submission of a task to execution of the task ,which results if more loose coupled and flexible design than tightly coupled create and execute pattern. Here are some more benefits of using Thread pool in Java:

1) Use of Thread Pool reduces response time by avoiding thread creation during request or task processing.
2) Use of Thread Pool allows you to change your execution policy as you need. you can go from single thread to multiple threads by just replacing ExecutorService implementation.

3) Thread Pool in Java application increases the stability of the system by creating a configured number of threads decided based on system load and available resource.

4) Thread Pool frees application developer from thread management stuff and allows to focus on business logic.

That's all on Thread pool in Java 5. we have seen what is thread pool in Java, what is executor framework in java 5, how to create thread pool in Java and some benefits of using thread pool in Java application. no doubt knowledge of thread pool is essential for a server side core Java developer and I suggest reading Java Threads and Concurrency Practice in Java to learn more about concurrency and thread pool.

Further Learning
Java Fundamentals Part 1,2
Java Concurrency in Practice
Applying Concurrency and Multi-threading to Common Java Patterns


vineet said...

Don't forget to call service.shutdown() on ExecutorService. Also why @Override on the run() gives a compile error.

Manoj Sharma said...

Thanks Javin, very nice article, this is really what I was looking from many days about executor framework. Good job done. Best of luck

Anonymous said...

what will happen if one of the thread in thread pool throws exception.

Anonymous said...

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Task implements Runnable {
private int id;

public Task(int id) {
this.id = id;

public void run() {
try {
System.out.println(Thread.currentThread().getName() + "----" + id);
} catch (Exception e) {
// TODO Auto-generated catch block


public class ThreadPool {

public static void main(String[] args) {
ExecutorService tPool = Executors.newFixedThreadPool(5);

for (int i = 0; i < 20; i++) {
tPool.submit(new Task(i));

try {
tPool.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {


Sapna Uniyal said...

How to decide the threadPool size? What are the factors(memory, processor, task incoming rate to be processed by the worker threads) to be considered when we decide the threadPool size?

Javin Paul said...

@Sapna Uniyal
Two factors
- load, how much message you are receiving per second
- number of CPU to avoid thread context switch

Post a Comment