Understanding Java ThreadPoolExecutor: Constructors, Parameters, and Usage
This article provides a comprehensive, beginner‑friendly guide to Java's ThreadPoolExecutor, explaining its constructors, core and maximum pool sizes, keep‑alive time, work queues, thread factories, rejection policies, and common factory methods such as newFixedThreadPool, newCachedThreadPool, and newSingleThreadExecutor, complete with code examples and flowcharts.
Sometimes you spend a lot of time reading something but still don’t understand it; investing time is still worthwhile.
Who this article is for
People who have heard of thread pools but are still fuzzy about the details.
Readers who know most of the concepts but still have lingering doubts.
Who this article is not for
Complete beginners – you should read more fundamental articles first.
Those who have forgotten most of the material – review basics before proceeding.
What you will gain
For suitable readers, a thorough understanding of the commonly used thread‑pool concepts.
For unsuitable readers, at least a solid conceptual overview.
Let’s dive in. Below are the key code snippets and diagrams you can save for repeated reference.
Default Constructor
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
{ ... }Explanation of the constructor parameters:
When the number of threads is less than corePoolSize, a new thread is created for each submitted task, even if idle threads exist.
When the thread count reaches corePoolSize, new tasks are placed into workQueue for later execution.
If workQueue is full and maximumPoolSize > corePoolSize, additional tasks cause new threads to be created.
If the queue is full and the task count exceeds maximumPoolSize, the task is handled by the RejectedExecutionHandler.
When the number of threads exceeds corePoolSize and idle time surpasses keepAliveTime, those excess threads are reclaimed.
If allowCoreThreadTimeOut(true) is set, even core threads are reclaimed after being idle for keepAliveTime.
General Flowchart
newFixedThreadPool Flowchart
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(
nThreads, // corePoolSize
nThreads, // maximumPoolSize == corePoolSize
0L, // keepAliveTime = 0
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>() // unbounded queue
);
}newCachedThreadPool Flowchart
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(
0, // corePoolSize == 0
Integer.MAX_VALUE, // very large maximumPoolSize
60L, // keepAliveTime = 60 seconds
TimeUnit.SECONDS,
new SynchronousQueue<Runnable>() // hand‑off queue
);
}newSingleThreadExecutor Flowchart
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(
new ThreadPoolExecutor(
1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory
)
);
}The only difference from newFixedThreadPool with nThreads = 1 is the additional FinalizableDelegatedExecutorService wrapper, which calls shutdown() in its finalize() method, ensuring the executor is terminated when garbage‑collected.
Using a custom ThreadFactory allows you to set thread names, groups, priorities, or daemon status, though the default factory is usually sufficient.
Finally
There is also a scheduled thread‑pool, ScheduledThreadPoolExecutor, used for delayed or periodic tasks, though it is less frequently needed.
(End)
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.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
