Java ThreadPoolExecutor

In this tutorial, we will learn about java.util.concurrent.ThreadPoolExecutor class, its features with explanation including with the working examples of it.

java.util.concurrent.ThreadPoolExecutor

In Java, ThreadPoolExecutor is a class from java concurrent package provides the implementation to the ExecutorService interface which gives control over the management of threads in a thread pool. It provides a configurable pool of worker threads that can execute tasks concurrently, improving the performance of the applications by reducing the overhead of thread creation and destruction.

Features of ThreadPoolExecutor

  1. It manages a pool of worker threads for executing tasks concurrently, thereby reducing the overhead of creating and destroying threads for each task.
  2. The ThreadPoolExecutor allows customization of the core pool size, maximum pool size, and queue capacity, allowing the developer to fine-tune the pool according to the specific requirements of the application and also based on the hardware resources.
  3. ThreadPoolExecutor allows the developer to customize the thread creation policy by providing a ThreadFactory implementation.
  4. It also provides a way to handle situations where the pool is full and cannot accept new tasks, by providing a RejectedExecutionHandler implementation.
  5. It provides hooks for customization of the thread termination policy, allowing the developer to perform any necessary clean-up or resource to release when a thread is terminated or exited.

ThreadPoolExecutor example with pool size:

In this example, we will submit 10Tasks to the ThreadPoolExecutor with the pool size of 2.

CopiedCopy Code
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolExecutorExample {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
        for (int i = 0; i < 10; i++) {
            executor.execute(() -> {
                try {
                    System.out.println("Task is executing by thread :"+Thread.currentThread().getName());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executor.shutdown();
    }
}

Output :

Task is executing by thread :pool-1-thread-1
Task is executing by thread :pool-1-thread-2
Task is executing by thread :pool-1-thread-1
Task is executing by thread :pool-1-thread-2
Task is executing by thread :pool-1-thread-1
Task is executing by thread :pool-1-thread-2
Task is executing by thread :pool-1-thread-1
Task is executing by thread :pool-1-thread-2
Task is executing by thread :pool-1-thread-1

Explanation :

In the above example, we have created a new ThreadPoolExecutor with a fixed pool size of 2 threads. And then submit 10 tasks, each task sleeping for 1 second and printing a message. The shutdown() method is called on the executor to shut down the thread pool after all the tasks have been completed otherwise the application will never exit.

Conclusion:

In this tutorial, we have covered what is ThreadPoolExecutor and its features along with the working example of it.