Java Thread pools.

Hashan Mahesh
4 min readSep 13, 2019

Suppose there is server software that creates new threads for each and every client request. When there are a million number of clients are requesting different tasks through the server, the server must create million of threads for them which is a very expensive operation and could limit throughput to cause poor performance.

The solution for this problem is solved by the Thread pools in Java. Simply, a thread pool is a collection of pre-initialized threads. These threads will be applied to a given set of tasks, no new threads will be created. It facilitates the execution of N number of tasks using the same threads. If there are more tasks than threads, then tasks need to wait in a queue-like structure (FIFO-First in First Out).

Java concurrency API provides the Executor Framework for creating Thread pools. Executor Framework contains the Executor interface, its sub-interface Executor Service, and the ThreadPoolExecutor class that implements both interfaces.

Types of Thread pool executors. There are 5 types of thread pool executors with pre-built methods

  1. Fixed thread pool executor.
  2. Cached thread pool executor.
  3. Scheduled thread pool executor.
  4. Single thread pool executor.
  5. Work stealing thread pool executor.

Fixed thread pool executor.

A fixed number of threads is used to execute any number of tasks. If there are more tasks are available than the number of threads, then the rest have to wait in a queue until a thread is available.

// Syntax 
ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(10);

NOTE| number of threads are given inside the brackets.

Cached thread pool executor.

Here, the number of threads is not fixed. When there are more requests are coming to use threads, then the Cached thread pool executor creates new threads for them instead of putting them on a waiting queue like a Fixed thread pool executor.

This mechanism can bring the system down if the number of threads goes beyond what the system can handle.

//Syntax 
ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool();

Scheduled thread pool executor.

A ThreadPoolExecutor can additionally schedule commands to run after a given delay or to execute periodically. Simply, given tasks will be run according to a schedule.

//Syntax 
ScheduledThreadPoolExecutor executor = Executors.newScheduledThreadPool(10);

NOTE|core pool size must be given in the brackets.

Single thread pool executor.

This is equivalent to the newFixedThreadPool(1) which has only one thread to execute all the tasks. In the single thread pool executor, a single thread will be used to do multiple tasks. When there is more than one task are available, the rest will wait in a FIFO queue.

//Syntax
ExecutorService executor = Executors.newSingleThreadExecutor();

Work stealing thread pool executor.

Here, the thread pool maintains enough threads to support the given parallelism. What I meant by parallelism is the number of threads that execute parallel (or at the same instance of time) on CPU cores.

//Syntax ExecutorService executor = Executors.newWorkStealingThreadPool(2)

NOTE| The number of threads that we need to execute parallaly is given in the parenthesis.

let’s code

The following program shows you how you can use two threads to execute 6 tasks. This program executes its tasks two by two since it has only 2 threads in the fixed thread pool.

OUTPUT>>

Task created 
Task created
Task created
Task created
Task created
Task created
Task0
Task1
Task2
Task3
Task4
Task5

The shutdown() method at the end of the above program prevents the main thread to send more tasks to the executor service. If you do not call this method at the last of your program after using the executor services, the program will not be stopped.

In the above program, we have used a newFixedThreadPool(2) which has only 2 threads in it, but instead, you can use the newCachedThreadPool() type to execute all these 6 tasks at once by creating new threads in the pool, or the newSingleThreadExecutor() method can be used to execute these 6 tasks one by one.

Scheduled Thread Pool Executors.

The following program has 6 tasks, which are scheduled by the Scheduled Thread Pool Executor.

OUTPUT>>

Task created 
Task created
Task created
Task created
Task created
Task created
Task0
Task1
Task2
Task3
Task4
Task5 // executes for 5 times with a 2sec time interval

this program is executed periodically for the given initial and period time units in second for the given tasks.

With these thread pool types, you can create awesome Java projects that enhance performance.

--

--

Hashan Mahesh

I am an undergraduate at the University of Kelaniya, Sri Lanka. I study Information Communication Technology including Artificial Intelligence technologies