Thread Pool in Java

Last Updated : 25 Nov, 2026

A thread pool in Java is a collection of reusable worker threads that execute submitted tasks. It reduces the overhead of repeatedly creating and destroying threads.

  • Available worker threads execute submitted tasks.
  • Tasks that cannot execute immediately may wait in a task queue.
  • Worker threads can be reused after completing their assigned tasks.

Thread Pool Initialization

A fixed thread pool can be created using the newFixedThreadPool() method.

Syntax:

ExecutorService pool = Executors.newFixedThreadPool(3);

  • Worker threads are created as needed, up to the configured pool size.
  • An unbounded task queue holds tasks waiting for execution.
  • Available workers execute submitted tasks.

Working of a Thread Pool

Consider a thread pool with three worker threads and five tasks. The following steps illustrate task assignment and thread reuse using an already initialized pool.

Step 1: Idle State

  • Three worker threads are idle and ready to execute tasks.
  • Five tasks are available in the task queue for execution.
Thread-Pool-1
Thread Pool with Idle Workers

Step 2: Task Assignment

  • Thread 1 executes Task 1.
  • Thread 2 executes Task 2.
  • Thread 3 executes Task 3.
  • Task 4 and Task 5 wait in the task queue.
Thread-Pool-2
Thread Pool executing first three tasks

Step 3: Thread Reuse

  • After completing their tasks, Thread 1 and Thread 2 pick up Task 4 and Task 5.
  • Thread 3 remains idle because no additional tasks are waiting.
  • The task queue becomes empty.
Thread-Pool-3
Thread Pool executing task 4 and 5

Note: These images illustrate one possible task execution sequence. Actual task assignment and completion order may vary.

Example: Creating a Thread Pool Using ExecutorService

The following program creates a fixed thread pool with three worker threads and submits five tasks.

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

public class ThreadPoolDemo{
    public static void main(String[] args)
        throws InterruptedException{

        // Create a fixed thread pool
        
        ExecutorService pool = Executors.newFixedThreadPool(3);

        // Submit five tasks
        
        for (int i = 1; i <= 5; i++){
            int taskId = i;

            pool.submit(() ->{
                System.out.println("Executing Task " + taskId
                    + " by " + Thread.currentThread().getName());
            });
        }

        // Stop accepting new tasks
        
        pool.shutdown();

        // Wait for submitted tasks to finish
        
        if (pool.awaitTermination(10, TimeUnit.SECONDS)) {
            System.out.println("All tasks completed.");
        } else {
            pool.shutdownNow();
        }
    }
}

Output:

Screenshot-2026-11-24-113351

Explanation:

  • newFixedThreadPool(3) creates a pool with up to three worker threads.
  • submit() submits tasks for execution.
  • Worker threads execute and reuse their capacity for subsequent tasks.
  • shutdown() stops accepting new tasks while allowing submitted tasks to finish.
  • awaitTermination() waits for the executor to terminate within the specified timeout.

The execution order and thread assignment may vary.

Common Types of Thread Pools

The Executors class provides factory methods for creating different thread pools.

Thread Pool Type

Factory Method

Description

Fixed Thread Pool

newFixedThreadPool()

Uses a fixed maximum number of worker threads.

Cached Thread Pool

newCachedThreadPool()

Creates threads as needed and reuses idle threads.

Single Thread Executor

newSingleThreadExecutor()

Executes tasks sequentially using one worker thread.

Scheduled Thread Pool

newScheduledThreadPool()

Executes tasks after a delay or periodically.

Work-Stealing Pool

newWorkStealingPool()

Distributes tasks among workers using work stealing.

Benefits of Thread Pool

  • Reduces thread creation and destruction overhead through thread reuse.
  • Controls concurrent thread execution based on the pool configuration.
  • Simplifies task scheduling and thread lifecycle management.
  • Supports concurrent execution of independent tasks.

Thread pools help manage resources but do not guarantee protection against resource exhaustion.

Common ExecutorService Methods

Method

Description

execute(Runnable command)

Executes a task without returning a Future.

submit(Runnable task)

Submits a task and returns a Future.

submit(Callable<T> task)

Submits a task that returns a result through a Future<T>.

shutdown()

Stops accepting new tasks while allowing submitted tasks to complete.

shutdownNow()

Attempts to interrupt running tasks and returns waiting tasks.

isShutdown()

Checks whether shutdown has been initiated.

isTerminated()

Checks whether all tasks have completed following shutdown.

awaitTermination(long timeout, TimeUnit unit)

Waits for executor termination up to the specified timeout.

Comment