Understanding ThreadPoolExecutor Parameters and Working Mechanism in Java
This article explains the core concepts, configurable parameters, and execution workflow of Java's ThreadPoolExecutor, illustrating each setting with analogies, code examples, and step‑by‑step analysis to help developers design safe and efficient thread pools.
Preface
Among many multithreaded Runnable executors, the most commonly used is ThreadPoolExecutor, i.e., a thread pool.
ThreadPoolExecutor
Most developers have used ThreadPoolExecutor. Some create it directly with new ThreadPoolExecutor, while others use factory methods such as Executors.newFixedThreadPool. Alibaba's BB guidelines forbid using Executors to create thread pools because hidden default parameters may cause resource exhaustion.
ThreadPoolExecutor Parameters
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) { ... } corePoolSize– number of core threads (permanent employees). maximumPoolSize – maximum number of threads (including temporary workers). keepAliveTime & unit – time that idle temporary threads stay alive. workQueue – task queue (to‑do list). threadFactory – source of threads (factory for employees). handler – rejection policy (how to handle overflow tasks).
Diagram Understanding
We compare the thread pool to a company: core threads are regular staff, the work queue holds pending tasks, and temporary threads are hired when the queue is full. When the pool reaches maximumPoolSize, further tasks are rejected and processed by the handler.
Working Mechanism
When the pool is created, there are no threads. The execution flow is:
step1
If the current worker count is less than corePoolSize, a new core thread is created and the task is executed; the thread remains alive even after the task finishes, waiting for more work from the queue.
step2
If the core limit is reached, the task is offered to workQueue. Idle threads can later take tasks from the queue.
step3
If the queue is full and the total worker count is still below maximumPoolSize, a temporary thread is created. Temporary threads die after keepAliveTime of idleness.
Code
public interface ThreadFactory {
// create a new thread
Thread newThread(Runnable r);
}A custom ThreadFactory can give threads a unified name prefix, e.g.:
private static ThreadFactory threadFactory = new ThreadFactory() {
private AtomicInteger no = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "my-thread-" + no.incrementAndGet());
}
}; // generated thread names: my-thread-1, my-thread-2, ...The core execution method ThreadPoolExecutor.execute(Runnable command) follows three steps (simplified):
public void execute(Runnable command) {
if (command == null) throw new NullPointerException();
int c = ctl.get();
// step1: if workerCount < corePoolSize, add core worker
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true)) return;
c = ctl.get();
}
// step2: try to queue the task
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (!isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
} else {
// step3: try to add a non‑core (temporary) worker
if (!addWorker(command, false))
reject(command);
}
}The addWorker method actually creates the thread via the supplied ThreadFactory:
private void addWorker(Runnable firstTask, boolean core) { ... }Summary
ThreadPoolExecutor involves many thread‑safety considerations; the author plans to implement a custom thread pool to deepen understanding.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
