Quick revision
The short answer
When a task is submitted, ThreadPoolExecutor first creates a new thread if fewer than the core number are running; otherwise it offers the task to the queue; only if the queue refuses because it is full does it create extra threads up to the maximum; and if the pool is at the maximum and the queue is full, the rejection handler runs. An unbounded queue never refuses a task, so with core 10, max 50 and an unbounded queue the pool stays at 10 threads forever. Threads above the core size end after keepAliveTime when idle, and allowCoreThreadTimeOut(true) applies that timeout to core threads too.
What to remember
In this answer, we followed ThreadPoolExecutor's decision for each task: create a thread while below the core size, otherwise offer to the queue, create threads up to the maximum only when the queue refuses, and reject when both are full. Because an unbounded queue never refuses, a pool with core 10 and max 50 stays at 10 threads, which we proved with getLargestPoolSize(). With a bounded queue the pool grows to 50 and then rejects, and threads above the core size end after keepAliveTime, or core threads too when allowCoreThreadTimeOut is enabled.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
When a task is submitted, ThreadPoolExecutor first creates a new thread if fewer than the core number are running; otherwise it offers the task to the queue; only if the queue refuses because it is full does it create extra threads up to the maximum; and if the pool is at the maximum and the queue is full, the rejection handler runs. An unbounded queue never refuses a task, so with core 10, max 50 and an unbounded queue the pool stays at 10 threads forever. Threads above the core size end after keepAliveTime when idle, and allowCoreThreadTimeOut(true) applies that timeout to core threads too.
Why this question is asked
Most developers expect a thread pool with a maximum of 50 to grow towards 50 when it gets busy. ThreadPoolExecutor does something else, and the difference has caused many slow services and a few outages. Interviewers ask this question because it shows whether we know the actual algorithm inside the executor that every Spring application and most libraries use. In this answer, we will recap the parameters, follow the decision execute() makes for each task, prove the core 10, max 50 surprise with code, see what changes with a bounded queue, and look at how idle threads are removed.
1A Quick Recap of the Parameters
A ThreadPoolExecutor is configured with a core pool size, the number of threads it keeps even when idle; a maximum pool size, the upper limit of threads; a keepAliveTime, how long a thread above the core size may stay idle before it ends; a work queue, where tasks wait for a thread; and a rejection handler, which decides what happens to a task the pool cannot accept. Threads are created lazily, as tasks arrive, unless we call prestartAllCoreThreads().
2The Decision Made for Every Task
Every call to execute(), which submit() also uses, runs the same three-step check. The order is the whole point.
- Fewer than core threads running?YesStart a new thread with this taskNoOffer the task to the queue
- Did the queue accept it?YesTask waits for a free threadNoTry to add a thread above core
- Fewer than max threads running?YesStart an extra thread with this taskNoRun the rejection handler
- Idle extra threads end after keepAliveTimecore threads too if
allowCoreThreadTimeOut(true)
4 steps in order. Play them, or step through them one at a time.
The queue is tried before any thread above the core size is created.
First, if fewer than core threads are running, the executor starts a new thread with this task as its first task, even if other core threads are idle. Second, if the core threads all exist, it calls queue.offer(task), which for a bounded queue returns false when the queue is full and for an unbounded queue always returns true. Third, only if offer returned false does the executor try to add a thread beyond the core size, which succeeds as long as the pool is below the maximum. If that fails too, the task is rejected and the handler runs; the default, AbortPolicy, throws RejectedExecutionException.
Notice the design choice: the executor prefers queueing over creating threads. The reasoning is that threads are expensive, so extra threads are a last resort for bursts that a bounded queue cannot absorb. That is reasonable for CPU work, but it surprises people who expected the maximum to be used first.
3Proving the Core 10, Max 50 Surprise
Let us build the pool from the question and give it 1,000 slow tasks.
The first ten tasks each start a core thread. Task 11 finds ten threads running, so it is offered to the queue, and the unbounded LinkedBlockingQueue accepts it. So do tasks 12 to 1,000. Because offer never fails, step three never runs, and the pool never creates thread 11. The 1,000 tasks take 100 rounds of 5 seconds, over eight minutes, while 40 permitted threads are never started. The maximum pool size is simply dead configuration whenever the queue is unbounded, which is exactly how Executors.newFixedThreadPool is built, with core equal to max to make that honest.
4The Same Pool With a Bounded Queue
Now give the pool a queue with capacity 100 and submit 200 long tasks.
Tasks 1 to 10 create core threads. Tasks 11 to 110 fill the queue. Tasks 111 to 150 find the queue full and create threads 11 to 50. Tasks 151 to 200 find the queue full and the pool at its maximum, so they are rejected. Notice the strange order this produces: tasks 111 to 150 start running immediately on new threads, while tasks 11 to 110, which arrived earlier, are still waiting in the queue. If we want threads to grow before tasks queue, a common approach is a SynchronousQueue, which holds no tasks at all, or a custom queue whose offer refuses while the pool is below its maximum.
5How Idle Threads Go Away
Threads above the core size are temporary. A worker thread waits for its next task with queue.poll(keepAliveTime) instead of take(). If the poll times out and the pool has more than core threads, the worker ends, so after a burst our 50-thread pool shrinks back to 10 after 60 idle seconds. Core threads normally wait with take() and live forever. Calling allowCoreThreadTimeOut(true) makes them use the timed poll too, so an idle pool can shrink to zero threads, which is useful for pools that are busy rarely, such as a nightly job's executor, at the cost of creating threads again on the next burst.
6Follow-up questions
How does Spring's ThreadPoolTaskExecutor behave?
It wraps the same ThreadPoolExecutor, so the same rules apply. Its default queue capacity is Integer.MAX_VALUE, which means its maxPoolSize has no effect unless we set a queueCapacity. In Spring Boot, the auto-configured task executor's pool and queue sizes come from spring.task.execution.pool properties; check the defaults for your version.
Why does execute() create a new core thread even if another core thread is idle?
Because checking whether a thread is idle would need coordination on every submission, while counting threads is cheap. Below the core size, the executor simply grows. Once at the core size, idle threads pick up tasks from the queue, so the effect only matters during warm-up, and prestartAllCoreThreads() removes it.
What happens to tasks in the queue when we call shutdown()?
shutdown() stops accepting new tasks but lets the threads finish everything already queued. shutdownNow() interrupts running tasks and returns the queued tasks that never started, so the caller can decide what to do with them.
7Conclusion
In this answer, we followed ThreadPoolExecutor's decision for each task: create a thread while below the core size, otherwise offer to the queue, create threads up to the maximum only when the queue refuses, and reject when both are full. Because an unbounded queue never refuses, a pool with core 10 and max 50 stays at 10 threads, which we proved with getLargestPoolSize(). With a bounded queue the pool grows to 50 and then rejects, and threads above the core size end after keepAliveTime, or core threads too when allowCoreThreadTimeOut is enabled.
Open with
For every task, ThreadPoolExecutor makes the same three checks in order. If fewer than the core number of threads are running, it starts a new thread for this task, even if another core thread happens to be idle.
Then, if they want more
Otherwise it offers the task to the work queue. Only if the queue refuses, because it is bounded and full, does it create an extra thread, up to the maximum pool size. If it is already at the maximum and the queue is full, the rejection handler runs, by default throwing RejectedExecutionException. So the pool prefers queueing over creating threads.
That is why core 10, max 50 with an unbounded queue never grows beyond 10: an unbounded LinkedBlockingQueue always accepts the offer, so the third step never happens and the maximum is dead configuration. If I submit 1,000 slow tasks, the pool size stays 10 and 990 tasks wait.
With a bounded queue of 100 and 200 tasks, I get 10 core threads, 100 queued, 40 extra threads up to 50, and 50 rejections. For idle threads, workers above the core size wait with a timed poll, and if keepAliveTime passes with no task, they end.
Core threads stay unless I call allowCoreThreadTimeOut(true), which applies the same timeout to them. Spring's ThreadPoolTaskExecutor wraps the same class, with an unbounded queue by default.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how ThreadPoolExecutor decides between creating a thread, queueing a task and...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/1f1fbc6542/