Inside Java ThreadPool: How execute() Decides Where Tasks Go
This article explains the step‑by‑step decision process a Java thread pool follows when execute() adds a task, covering core and maximum pool sizes, queue handling, thread creation, rejection policies, and idle thread termination.
When the execute() method submits a task to a thread pool, the pool follows a specific decision process.
1. If the number of active threads is less than corePoolSize, a new core thread is created to run the task.
2. If the active thread count is greater than or equal to corePoolSize, the pool checks whether the work queue is full.
If the queue is not full, the task is placed into the queue to wait for execution.
If the queue is full, the pool compares the current thread count with maximumPoolSize:
If the active threads are less than maximumPoolSize, a non‑core thread is created to handle the task.
If the active threads are greater than or equal to maximumPoolSize, the task is processed according to the pool’s rejection policy.
3. When a thread finishes its task, it retrieves the next task from the queue.
4. If a thread remains idle for longer than keepAliveTime and the total number of threads exceeds corePoolSize, the idle thread is terminated.
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.
