Java Concurrency in a Nutshell : Types of Thread Pools (Part 3)

Somnath Musib
Quick Code
Published in
3 min readSep 21, 2018

--

Introduction

In previous article, we discussed about basics of thread pool. We have provided an overview of Executor Service, Callable, Future and Thread pool executor. In this article, we will examine different types of built-in and commonly used thread pools provided by Java.

Photo by Tim Johnson on Unsplash

FixedThreadPool

This is a general purpose thread pool where thread pool is initialized with user supplied number of threads (nThreads). At any point in time, this thread pool contains nThreads no of threads. It internally manages a LinkedBlockingQueue to queue up additional influx of tasks when all threads in the pool are busy and new tasks can not be processed immediately.

A fixed thread pool can be created by calling newFixedThreadPool() of Executors class. In below code snippet a thread pool with 10 threads is created.

ExecutorService executorService = Executors.newFixedThreadPool(10);

Once a thread pool is created, any number of runnable or callable tasks can be submitted to it for processing.

executorService.submit(() -> {
System.out.println("Hello world");
});

Once all tasks are done, pool will still be active until it is explicitly shutdown. Java provides two shutdown methods to bring down the pool.

Invoking shutdown() on a pool will preventing the pool from accepting new tasks but allows currently running tasks to finish. Invoking shutdownow() prevents accepting new tasks as well as tries to cancel currently running tasks.

Below code snippet describes the usage of a Fixed thread pool:

Java provides two implementation to create a fixed thread pool. We have already seen the default one where user only requires to provide the number of threads with which this pool needs to be created. In this implementation, Java creates the threads with its internal thread factory.

In another implementation, Java provides the user to supply the custom ThreadFactory implementation to customize the thread creation. This version is useful when user wants to customize certain aspects of thread creation. For example, user would like to change the thread group, thread pool naming patterns, thread priorities and so on.

Following example shows implementation of a custom application thread and custom application thread factory:

Custom Application Thread

Following code snippet describes creation of a custom thread factory:

Custom Thread Factory

Following example shows the usage of fixed thread with custom thread factory implementation:

Fixed thread pool with custom thread factory

CachedThreadPool

Cached thread pool is another variant of thread pool provided by Java for common usage. A cached thread pool is useful when tasks submitted for processing should not wait and needs to be addressed as soon as submitted. To satisfy this requirement, Java creates a new thread for the submitted task if there are no thread in the pool to address the task. A cached thread pool can have up to 2³¹ no of threads. Effectively, this pool is for short lived asynchronous tasks.

Cached thread pool uses a SynchronousQueue to buffer the tasks if there are no active thread in the pool. Once a new task is submitted, a new thread is spawned and consumes the submitted task for processing. In a SynchronousQueue each insert operation must wait for a corresponding remove operation by another thread, and vice versa. A synchronous queue does not have any internal capacity, not even a capacity of one. This means as soon as a task is submitted, a thread needs to consume the task. A thread in cached thread pool can be idle for 60 seconds before it is terminated.

A cached thread pool is created by calling newCachedThreadPool() of Executors class.

ExecutorService executorService = Executors.newCachedThreadPool();

We can also supply custom thread factory class to override Java default thread factory and provide an implementation of our own:

ExecutorService executorService = Executors.newCachedThreadPool(new AppThreadFactory(CUSTOM_CACHED_POOL));

Following example shows the usage of both the thread pools with default and custom thread factory implementation:

Cached thread pool example

Cached thread pool with custom thread factory:

Cached thread pool with custom thread factory

SingleThreadPool

Often it requires to have a single thread up & running and process the submitted tasks. This can be done using fixed thread pool executor by setting the number of threads in the pool to 1. However, Java already provides a built-in thread pool solely for this purpose. In Singlethreadpool, pool can have maximum one thread and all submitted jobs are executed in sequence of submission.

--

--

Somnath Musib
Quick Code

Software Developer, Cloud Architect | Author "Spring Boot In Practice" . Find more at https://musibs.github.io