ThreadPoolExecutor
core pool size
maximum pool size
Java concurrency
thread management

Core pool size vs maximum pool size in ThreadPoolExecutor

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In a Java application, managing threads efficiently is crucial, especially when handling concurrent tasks. Java's ThreadPoolExecutor is a powerful tool that helps in managing a pool of threads for executing tasks. A ThreadPoolExecutor optimizes thread management by reusing threads and reducing the overhead of thread creation and destruction. Two important parameters in the ThreadPoolExecutor are core pool size and maximum pool size. Understanding these parameters is essential for effective concurrency management. Let's delve into these parameters in detail.

Core Pool Size

Definition

The core pool size is the minimum number of threads that should remain active, even if they are idle, within the pool. Once the threads are created and become part of the pool, they will not terminate unless explicitly configured to do so (e.g., through allowing core threads to timeout).

Technical Explanation

  • Threads in the core pool size are created and maintained without regard to task demand.
  • If a new task is submitted and fewer than the core number of threads are active, the ThreadPoolExecutor generally creates a new thread to handle the request, even if other worker threads are idle.

Example

Let's say you define a ThreadPoolExecutor with a core pool size of 3. Even if all submitted tasks are completed and no new tasks are coming in, these three threads will remain alive and idle, ready to process incoming tasks immediately without any delay induced by thread creation.

Maximum Pool Size

Definition

The maximum pool size is the upper limit of threads that can be active at any time in the ThreadPoolExecutor. The executor will not exceed this number of threads, and tasks beyond this limit will be queued.

Technical Explanation

  • The maximum pool size dictates how many threads, at most, can be alive and running at the same time.
  • If the number of threads is equal to or greater than the core pool size but less than the maximum pool size, a new thread is created only when the work queue is full.

Example

If a ThreadPoolExecutor is configured with a core pool size of 3 and a maximum pool size of 6, and there are currently four tasks in the queue, a fourth worker thread will be created since the current pool size is less than the maximum pool size.

How Core Pool Size and Maximum Pool Size Work Together

Workflow

  1. Task Submission - When a task is submitted...
    • If fewer than the core pool size, a new thread is created.
    • If equal to or more than the core pool size, the task is queued if the queue allows it.
  2. Queue Full - If the work queue is full...
    • A new thread is created unless the current pool size is at maximum.
  3. Rejection - If more tasks are submitted, and both the queue and the pool are full, the task will be rejected according to the RejectedExecutionHandler.

Key Points

  • Core pool threads are kept alive indefinitely unless allowCoreThreadTimeOut(true) is set.
  • Maximum pool size acts as a ceiling limiter, preventing spawning of excessive threads that could lead to overwhelming system resources.
  • An optimal balance should be found between core and maximum pool size to efficiently handle tasks without causing unnecessary delays or resource starvation.

Example: Configuring ThreadPoolExecutor

java
1ThreadPoolExecutor executor = new ThreadPoolExecutor(
2    3,                  // core pool size
3    6,                  // maximum pool size
4    60,                 // idle time before terminating excess threads
5    TimeUnit.SECONDS,   // time unit
6    new ArrayBlockingQueue<>(2)  // work queue
7);

In this configuration:

  • Three core threads will be created initially.
  • Up to 2 tasks can be queued before additional threads from the pool (up to 6) start.
  • If 8 tasks are submitted, the system will handle them without rejection due to the queue and maximum pool configurations.

Additional Details

Tuning the Parameters

  • Core Pool Size: Ideally set to the number of server cores for compute-bound tasks or higher for IO-bound tasks.
  • Maximum Pool Size: Consider CPU time, memory constraints, and task nature.

Allow Core Thread Timeout

By default, core threads are never terminated. This can be configured to allow termination by setting:

java
executor.allowCoreThreadTimeOut(true);

This setting is useful in scenarios where you surf accounting for periods of no activity and wish to release resource threads.

Summary Table

ParameterDescriptionIdeal Usage
Core Pool SizeMinimum number of alive threads, including when idle.Should match the number of server cores for efficiency.
Maximum Pool SizeMaximum number of threads that the executor can support.Should be higher, based on task characteristics.
QueueTasks waiting for execution will be stored here if the core pool is full.Limit depends on the task demand versus resource constraints.
Allow Core TimeoutOption to allow core threads to terminate when idle.Useful in resource-constrained environments.

Conclusion

Understanding the core pool size and maximum pool size in ThreadPoolExecutor forms the basis of effectively managing tasks in concurrent programming. Setting these parameters adequately tailored to your application's workload and environment can significantly affect performance and resource utilization. Proper tuning can deliver an optimal balance between throughput and resource constraints, ensuring robust and efficient task execution.


Course illustration
Course illustration

All Rights Reserved.