In Java, the ThreadPoolExecutor class from the java.util.concurrent package can be used to create and manage thread pools. The constructor of the ThreadPoolExecutor class allows you to set multiple parameters, which collectively determine the behavior and performance of the thread pool. Here is a detailed explanation of these parameters:
1. corePoolSize
- Type: int
- Description: The number of threads that remain active (non-stopped) in the thread pool, even if they are idle.
- Impact: If the number of threads is less than the core pool size, new threads will be created even if other work queues are full.
2. maximumPoolSize
- Type: int
- Description: The maximum number of threads allowed in the thread pool.
- Impact: If the queue is full and the current number of threads is less than the maximum pool size, new threads will be created.
3. keepAliveTime
- Type: long
- Description: When the number of threads exceeds the core pool size, this is the maximum time that idle threads exceeding the core pool size will wait for new tasks before terminating.
- Unit: The time unit is specified by the following parameter.
4. TimeUnit
- Type: TimeUnit (enumeration)
- Description: The time unit for the keepAliveTime parameter, such as seconds (TimeUnit.SECONDS), milliseconds (TimeUnit.MILLISECONDS), etc.
5. workQueue
- Type: BlockingQueue<Runnable>
- Description: A blocking queue used to hold tasks waiting for execution.
- Impact: When all core threads are busy, new tasks will be placed in this queue to wait.
6. threadFactory
- Type: ThreadFactory
- Description: A factory used to create new threads.
- Impact: Allows customization of thread creation, such as setting thread names, priorities, etc.
7. handler
- Type: RejectedExecutionHandler
- Description: The handling strategy adopted when a task cannot be executed (for example, when the work queue is full and the number of threads has reached the maximum).
- Impact: Common rejection policies include ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardPolicy, and ThreadPoolExecutor.DiscardOldestPolicy.
Example Code
import java.util.concurrent.*;
public class ThreadPoolExample {
public static void main(String[] args) {
int corePoolSize = 5; // Core pool size
int maximumPoolSize = 10; // Maximum pool size
long keepAliveTime = 1; // Idle thread survival time
TimeUnit unit = TimeUnit.MINUTES; // Time unit
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10); // Work queue
ThreadFactory threadFactory = Executors.defaultThreadFactory(); // Thread factory
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy(); // Rejection policy
ThreadPoolExecutor executor = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
workQueue,
threadFactory,
handler
);
// Use the thread pool to execute tasks...
for (int i = 0; i < 20; i++) {
}
}
In this example, we create a thread pool with 5 core threads and a maximum of 10 threads. Idle threads will be terminated after 1 minute. An unbounded ArrayBlockingQueue is used as the work queue, with the default thread factory and rejection policy set.