Java ThreadPoolExecutor with BlockingQueue

Learn to use Java ThreadPoolExecutor in combination with BlockingQueue.

1. Creating ThreadPoolExecutor

A ThreadPoolExecutor is a type of ExecutorService that executes each submitted task using one of the threads from a thread pool. This class provides many flexible ways to create a pool of threads in different contexts.

1.1 Constructors

The following constructors can be used to create a thread pool executor instance based on our requirements.

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

The constructor arguments are:

  • corePoolSize – the number of threads to keep in the pool, even if they are idle.
  • maximumPoolSize – the maximum number of threads to allow in the pool.
  • keepAliveTime – when the number of threads is greater than the core, this is the maximum time an idle thread will wait for the new task.
  • unit – the time unit for the keepAliveTime argument.
  • workQueue – the queue to use for holding Runnable tasks before they are executed.
  • threadFactory – an optional factory to use when the executor creates a new thread.
  • handler – rejected task execution handler.

1.2. Custom ThreadPoolExecutor

Even without extending the ThreadPoolExecutor, we can use it very effectively. But, we will miss some extremely useful features in terms of controlling the execution flow.

For example, ThreadPoolExecutor class provides two excellent methods which I will highly recommend to override. These methods provide a very good handle on the execution lifecycle of a Runnable to be executed.

  • beforeExecute()
  • afterExecute()
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CustomThreadPoolExecutor extends ThreadPoolExecutor {

    public CustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
            long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        System.out.println("Perform beforeExecute() logic");
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        if (t != null) {
            System.out.println("Perform exception handler logic");
        }
        System.out.println("Perform afterExecute() logic");
    }
}

2. Creating BlockingQueue

A BlockingQueue is like another Queue implementations with additional capabilities. Any attempt, to retrieve something out of it, can be seen safe as it will not return empty. The consumer thread will automatically wait until BlockingQueue is not populated with some data. Once it fills, the thread will consume the resource.

A BlockingQueue may be used to transfer and hold the tasks to be executed by the thread pool. Blocking queues helps in many ways:

  • If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.
  • If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.
  • If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.

2.1. Queueing Strategies

The ThreadPoolExecutor support different kind of blocking queues. Each queue provides a different behavior to the processing of the tasks.

2.1.1. Direct Handoffs

This can be achieved with SynchronousQueue that does not have any internal capacity. We cannot insert a task (using any method) unless another thread is trying to take it.

When using the synchronous queue, when we attempt to queue a task then this will fail if no threads are immediately available to run it. If it still has not reached to maximumPoolSize thread then a new thread will be constructed. Else, the task will be rejected immediately.

2.1.2. Unbounded Queues

An unbounded queue (for example, LinkedBlockingQueue) causes new submitted tasks to wait in the queue when all (corePoolSize) threads are busy. Because tasks can wait for unlimited time, the executor needs not create new threads. So maximumPoolSize has no effect if this queue is used.

This style of queuing can be useful when a sudden burst of requests comes to the server. Although, this may lead to memory issues if requests continue to come faster than they are processed.

2.1.3. Bounded Queues

Bounded queues (for example, ArrayBlockingQueue) helps in managing the resources in a much better way. It provides mechanisms to control the number of threads as well as the tasks in the queues to prevent resource exhaustion.

For different scenarios, we can test custom pool sizes and queue sizes, and finally, use what is best suited for our usecase.

  • Using large queues and small pools minimizes the system overhead, but leads to low throughput.
  • Using small queues and large pools also keeps the CPU busy which also can lead to low throughput.
  • So finding a right balance between the queue size and pool size is important.

2.2. Handling Rejected Tasks

There may be situations when the submitted tasks cannot be executed by the executor service and thus have been rejected. Task rejection may occur when no more threads or queue slots are available because their bounds have been exceeded, or the executor has been shut down.

ThreadPoolExecutor provides the following 4 inbuild handlers to handle these rejected tasks. We can create our own custom handler as well.

  • AbortPolicy : This is the default policy. It causes the executor to throw a RejectedExecutionException.
  • CallerRunsPolicy : This policy runs the rejected task directly in the calling thread of the execute method. If the executor has been shut down, the task will be discarded.
  • DiscardOldestPolicy : This policy discards the oldest unhandled request and then retries execute. If the executor has been shut down, the task will be discarded.
  • DiscardPolicy : This policy silently discards the rejected task.
  • Custom Policy : We can implement the RejectedExecutionHandler interface and provide our own logic to handle the rejected tasks.

3. Using ThreadPoolExecutor with BlockingQueue

To demonstrate the usage of ThreadPoolExecutor with BlockingQueue, we have created one task DemoTask. This task does nothing. It simply waits for 500ms and then completes.

public class DemoTask implements Runnable {
    private String name = null;

    public DemoTask(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Executing : " + name);
    }
}

Now lets suppose we have total 100 tasks. We want to run them using ideally 10, and the maximum of 20 threads.

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DemoExecutor {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Runnable> blockingQueue =
                new LinkedBlockingQueue<Runnable>();

        CustomThreadPoolExecutor executor =
                new CustomThreadPoolExecutor(10, 20, 5, TimeUnit.SECONDS,
                        blockingQueue, new ThreadPoolExecutor.AbortPolicy());

        // Let start all core threads initially
        executor.prestartAllCoreThreads();

        for (int i = 1; i <= 100; i++) {
            blockingQueue.offer(new DemoTask("Task " + i));
        }

        executor.shutdown();
        executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
    }
}

Execute the above code and you will see all the tasks gets executed one by one.

Happy Learning !!

Leave a Reply

38 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.