Backend Development 12 min read

Java Thread Pool: Working Mechanism, States, and Rejection Policies

Java thread pools efficiently manage and reuse threads by applying a producer‑consumer model, transitioning through RUNNING, SHUTDOWN, STOP, and TERMINATED states, handling task queues, and employing configurable rejection policies such as Abort, CallerRuns, Discard, and DiscardOldest, while improving performance, resource utilization, and stability across web servers, asynchronous processing, and other concurrent workloads, distinct from connection pools which manage database connections.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Java Thread Pool: Working Mechanism, States, and Rejection Policies

Thread pool is a mechanism for managing and reusing threads, enabling developers to efficiently execute concurrent tasks. Using thread pools provides several benefits:

Resource Management: Thread pools effectively manage system resources by limiting concurrent task quantities and reusing threads, reducing the overhead of thread creation and destruction, and improving system resource utilization.

Performance Improvement: By properly configuring thread pool size and task queues, task execution can be optimized, reducing thread context switching costs and improving task execution efficiency and system throughput.

Avoiding Resource Exhaustion: Thread pools control the number of concurrent tasks, preventing system resource exhaustion caused by creating too many threads, thereby improving system stability and reliability.

Task Queuing: Thread pools use task queues to store pending tasks, ensuring sequential task execution and flexibly handling burst task volumes to prevent system overload.

Simplifying Concurrent Programming: Using thread pools simplifies the complexity of concurrent programming. Developers don't need to manually manage thread lifecycle and task scheduling; they simply submit tasks to the thread pool, reducing programming complexity and error possibilities.

Thread Pool Working Mechanism

The thread pool working mechanism can be viewed as an application of the producer-consumer model. In this model, tasks (producers) are submitted to the thread pool, and threads in the pool (consumers) retrieve tasks from the queue and execute them.

Developers submit tasks using ThreadPoolExecutor's submit() method.

The thread pool running state is checked; if not RUNNING, the task is directly rejected. The thread pool must ensure tasks are executed in RUNNING state.

Submitted tasks (typically implementing Callable or Runnable interfaces) are encapsulated into a FutureTask object, which implements the Future interface and allows retrieving task execution results.

If the number of core threads in the pool is less than corePoolSize, attempts are made to create new core threads to execute tasks.

If the current core thread count has reached corePoolSize, tasks are placed in the task queue, waiting for worker threads to retrieve and execute them.

If the queue is full and the current thread count is less than maximumPoolSize, attempts are made to create new non-core threads to execute tasks.

If the current thread count has reached maximumPoolSize, the rejection policy is applied.

After task execution completes, the thread pool returns a Future object through which the task execution result can be retrieved.

Thread Pool States

Java thread pools have different states reflecting various stages and behaviors in their lifecycle. The main thread pool states are:

State

Description

RUNNING

Indicates the thread pool is running normally and can accept new task submissions. In this state, the thread pool can execute tasks and create new threads to handle them.

SHUTDOWN

Indicates the thread pool is shutting down. In this state, the thread pool no longer accepts new task submissions but continues executing submitted tasks until all tasks are completed.

STOP

Indicates the thread pool has stopped. It no longer accepts new task submissions and attempts to interrupt tasks currently being executed.

TERMINATED

Indicates the thread pool has terminated. It no longer accepts new task submissions and all tasks have been executed. In this state, all threads in the thread pool have been destroyed.

These states are maintained through the ctl (control) field in the ThreadPoolExecutor class. ctl is an AtomicInteger variable where the high 3 bits represent the thread pool running state, and the low 29 bits represent the number of working threads in the pool.

In ThreadPoolExecutor, bit operations are used to modify and check ctl values to achieve thread pool state transitions and management.

Note that thread pool states are not set directly but are transitioned by calling methods like shutdown() and shutdownNow(). For example, calling shutdown() transitions the thread pool state from RUNNING to SHUTDOWN.

Rejection Policies

Thread pool rejection policies define actions to take when the thread pool is full and cannot handle newly submitted tasks. Common Java thread pool rejection policies include:

Policy Name

Description

AbortPolicy (Default)

If the thread pool is full and cannot accept new tasks, a RejectedExecutionException is thrown. This is the default rejection policy.

CallerRunsPolicy

When the thread pool is full, the thread that submitted the task executes the task. In other words, if a new task cannot be accepted, the submitting thread executes it.

DiscardPolicy

When the thread pool is full, new tasks that cannot be handled are discarded without throwing an exception.

DiscardOldestPolicy

When the thread pool is full, the longest-waiting task in the queue is discarded, and then the new task is attempted to be added to the queue.

Beyond the standard rejection policies, you can implement the RejectedExecutionHandler interface to define custom rejection policies, enabling more complex rejection logic based on application requirements:

public interface RejectedExecutionHandler {
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}

Thread Pool Usage Scenarios

Java thread pools have many practical business applications:

Web Servers: Using Tomcat as an example. Tomcat is a common Java web server that uses thread pools to handle incoming HTTP requests. Each time a new HTTP request arrives, Tomcat retrieves a thread from the pre-configured thread pool to handle it, effectively managing concurrent requests and improving server response speed and stability.

Concurrent Task Processing: Many business scenarios require processing large volumes of concurrent tasks, such as data processing, file upload/download, and message processing. Thread pools can be used for concurrent processing to improve task execution efficiency and system throughput.

Asynchronous Processing: In some business scenarios, time-consuming operations need to be executed without blocking the main thread. Thread pools can be used to asynchronously execute these operations, such as sending emails, SMS notifications, and data analysis. By submitting tasks to the thread pool, the main thread can return immediately while tasks execute asynchronously in background threads.

Difference Between Thread Pools and Connection Pools

A connection pool is a group of pre-initialized and reusable database connections. It manages the pool of connections to a database, allowing multiple clients to share and reuse database connections.

Connection pools help improve performance and scalability of database-intensive applications by reducing the overhead of establishing and closing database connections.

Thread pools and connection pools are both important technologies for improving system performance and resource utilization, but they differ mainly in application scenarios and the types of resources they manage.

Thread pools manage reusable thread resources for efficiently executing concurrent tasks, while connection pools manage reusable database connection resources for efficiently handling database access.

Javaperformance optimizationbackend developmentconcurrencythread poolJava ConcurrencyThreadPoolExecutor
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.