Mastering ThreadPoolExecutor: Understanding All 7 Constructor Parameters
This article breaks down the seven parameters of Java's ThreadPoolExecutor constructor, explaining corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, and handler, and provides practical tips on thread creation and rejection policies.
Parameter Explanation
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)corePoolSize (core thread count)
The thread pool creates no threads until a task is submitted; corePoolSize is the minimum number of threads kept alive even when idle. When a task arrives and the current running thread count is less than corePoolSize, a new core thread is created to handle it.
ThreadPoolExecutor provides prestartCoreThread() and prestartAllCoreThreads() methods to pre‑create core threads at pool creation time.
maximumPoolSize (maximum thread count)
This is the upper limit of threads (core + non‑core). When the BlockingQueue is full and the current thread count is below maximumPoolSize, new non‑core threads are created. If the thread count reaches maximumPoolSize and the queue is still full, new tasks trigger the rejection policy.
The maximumPoolSize value must be greater than or equal to corePoolSize . With an unbounded queue such as LinkedBlockingQueue , maximumPoolSize effectively has no impact because the queue never fills.
keepAliveTime (thread keep‑alive time)
When the number of threads exceeds corePoolSize, idle non‑core threads wait up to keepAliveTime for new tasks before being terminated.
By default, keepAliveTime applies only when the thread count is greater than corePoolSize . If allowCoreThreadTimeOut=true is set, core threads are also subject to this timeout.
unit (time unit)
The time unit for keepAliveTime, e.g., NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, etc.
workQueue (task queue)
A blocking queue that holds tasks awaiting execution. When the running thread count equals corePoolSize, new tasks are placed into the queue; if the queue is full and the thread count is still below maximumPoolSize, additional non‑core threads are created.
threadFactory (thread factory)
Factory for creating new threads, allowing customization such as thread name or priority. The default is Executors.defaultThreadFactory().
handler (rejection policy)
Strategy for handling tasks when both the pool and the queue are full. Common policies include AbortPolicy, CallerRunsPolicy, DiscardPolicy, and DiscardOldestPolicy.
Tips
Core thread creation timing
When a task is submitted and the current running thread count is less than corePoolSize, a core thread is created. Core threads are not destroyed by default unless allowCoreThreadTimeOut=true is set.
Non‑core thread creation timing
Non‑core threads are created only when the work queue is full and the current thread count is within [corePoolSize, maximumPoolSize). These threads are terminated after being idle for keepAliveTime.
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.
Xuanwu Backend Tech Stack
Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.
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.
