Quick revision
The short answer
When a task arrives, ThreadPoolExecutor first creates a new thread if fewer than corePoolSize threads exist; otherwise it offers the task to the queue; only if the queue refuses does it create extra threads up to maximumPoolSize; and if that also fails, the rejection handler runs. With new ThreadPoolExecutor(2, 10, 60, SECONDS, new LinkedBlockingQueue<>()), the queue is unbounded and never refuses, so 1,000 tasks submitted at once give exactly 2 running threads and 998 waiting tasks, and the maximum of 10 is never used.
What to remember
In this answer, we followed ThreadPoolExecutor's order of decisions: create a thread while below the core size, otherwise queue the task, and only if the queue refuses create threads up to the maximum, rejecting when that fails too. With an unbounded LinkedBlockingQueue the queue never refuses, so 1,000 tasks run on 2 threads with 998 waiting, and the maximum and keep-alive settings never apply. A bounded queue, equal core and maximum with core timeout, or a grow-first queue give the growth people usually expect.
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 arrives, ThreadPoolExecutor first creates a new thread if fewer than corePoolSize threads exist; otherwise it offers the task to the queue; only if the queue refuses does it create extra threads up to maximumPoolSize; and if that also fails, the rejection handler runs. With new ThreadPoolExecutor(2, 10, 60, SECONDS, new LinkedBlockingQueue<>()), the queue is unbounded and never refuses, so 1,000 tasks submitted at once give exactly 2 running threads and 998 waiting tasks, and the maximum of 10 is never used.
Why this question is asked
This is one of the most misunderstood parts of the JDK. Most people read the constructor as 'between 2 and 10 threads, growing with load', and set it up expecting exactly that. The real behaviour is different, and the difference has caused many slow services that never used more than their core threads. Interviewers ask it because it tests whether we know the actual algorithm rather than the intuition. In this answer, we will recap the parameters, walk through the decision order with the reason for it, run the 1,000-task example, and then look at configurations that behave the way people expect.
1A Quick Recap of the Parameters
The constructor takes corePoolSize, the number of threads the pool aims to keep; maximumPoolSize, the upper limit on threads; keepAliveTime and its unit, how long threads beyond the core size may stay idle before ending; and the work queue, which holds tasks waiting for a thread. Optional arguments give a thread factory and a rejection handler, which by default is AbortPolicy, throwing RejectedExecutionException.
The key to the whole question is the role of the queue. The pool does not look at how busy its threads are to decide whether to grow; it looks at whether the queue accepted the task.
2The Order of Decisions
execute(task) follows three steps. First, if fewer than corePoolSize threads are running, it creates a new thread with this task as its first task, even if other core threads are idle. Second, if the core is full, it offers the task to the queue; if the offer succeeds, the task waits there until some thread takes it. Third, only if the offer fails, which means the queue is full, it tries to create a new thread beyond the core, up to maximumPoolSize, again giving it this task directly. If the pool is already at the maximum, the task is rejected and the handler runs.
The reason for this order is a design preference: the pool tries to keep a stable number of threads and treats extra threads as an emergency measure for when the buffer overflows. Queuing is cheap; creating threads was expensive. That made sense, but it means growth beyond the core depends entirely on the queue being able to fill up.
- Fewer than core threads?YesCreate a thread for this taskNoOffer the task to the queue
- Queue accepted it?YesTask waits for a free threadNoTry to add a thread
- Fewer than max threads?YesCreate an extra thread for this taskNoReject: run the handler
3 steps in order. Play them, or step through them one at a time.
Extra threads beyond the core are created only when the queue refuses a task.
3Running the 1,000-Task Example
Now apply the steps to new ThreadPoolExecutor(2, 10, 60, SECONDS, new LinkedBlockingQueue<>()). A LinkedBlockingQueue created without a capacity holds up to Integer.MAX_VALUE tasks, so in practice its offer never fails.
Task 1 and task 2 each create a core thread. Task 3 finds the core full, offers itself to the queue, and the queue accepts. So does every task after it, all the way to task 1,000. Step three is never reached, so no extra thread is ever created, and maximumPoolSize and keepAliveTime have no effect at all. With 10 threads the batch would take about 10 seconds; with this configuration it takes about 50. Nothing fails and nothing logs an error, which is why this mistake survives in production for so long.
4Getting the Growth People Expect
If we want the pool to grow under load, there are a few honest options. The first is a bounded queue. With an ArrayBlockingQueue of capacity 100, the same 1,000 tasks behave very differently: tasks 1 and 2 create core threads, tasks 3 to 102 fill the queue, tasks 103 to 110 each create an extra thread, bringing the pool to 10, and the remaining 890 tasks are rejected.
| Configuration | Threads used | Outcome |
|---|---|---|
| core 2, max 10, unbounded queue | 2 | 998 queued, about 50 s total |
core 2, max 10, queue of 100, AbortPolicy | 10 | 100 queued, 890 rejected |
core 2, max 10, queue of 100, CallerRuns | 10 + caller | Producer slowed, nothing lost |
| core 10, max 10, queue of 1,000, core timeout | 10 | 990 queued, about 10 s, shrinks when idle |
Notice that the default AbortPolicy would throw RejectedExecutionException for those 890 tasks. CallerRunsPolicy instead runs each refused task on the submitting thread, which naturally slows the producer and acts as back pressure. The second option is to set core and maximum to the same value, for example 10 and 10, with a bounded queue, and call allowCoreThreadTimeOut(true) so idle threads still end after the keep-alive time. That gives 'up to 10 threads, then queue' with shrinking when idle, and it is often the simplest correct configuration. A third option, used by Tomcat, is a queue that refuses offers while the pool can still grow.
5Follow-up questions
Why does the pool create a new core thread even when others are idle?
Because step one only checks the number of threads, not whether they are busy. While the pool is below its core size, every new task starts a new thread. prestartAllCoreThreads() creates them all up front if we want them ready before the first task.
What happens with a core size of 0 and an unbounded queue?
The task is queued, and execute then notices that no thread is running and starts one, so the pool runs with a single thread that drains the whole queue. That is why such a configuration effectively behaves like a single-thread executor, not an elastic pool.
Does keepAliveTime matter in the original example?
No. It only applies to threads beyond the core size, or to core threads when allowCoreThreadTimeOut is true. Since the original pool never grows beyond 2 threads and the core does not time out, the 60 seconds never applies.
6Conclusion
In this answer, we followed ThreadPoolExecutor's order of decisions: create a thread while below the core size, otherwise queue the task, and only if the queue refuses create threads up to the maximum, rejecting when that fails too. With an unbounded LinkedBlockingQueue the queue never refuses, so 1,000 tasks run on 2 threads with 998 waiting, and the maximum and keep-alive settings never apply. A bounded queue, equal core and maximum with core timeout, or a grow-first queue give the growth people usually expect.
Open with
ThreadPoolExecutor decides in a fixed order. When a task arrives, if fewer than corePoolSize threads exist, it creates a new thread for that task, even if other threads are idle.
Then, if they want more
Otherwise it offers the task to the queue. Only if the queue rejects the offer, meaning it's full, does it create additional threads beyond the core, up to maximumPoolSize. And if it's already at the maximum, the rejection handler runs, which by default throws RejectedExecutionException. The important consequence is that growth beyond the core depends on the queue filling up.
With core 2, max 10 and a LinkedBlockingQueue with no capacity, the queue is effectively unbounded, so every offer succeeds. Submitting 1,000 tasks at once gives two core threads and 998 tasks in the queue, and the pool never creates the other eight threads, so max and keepAliveTime do nothing.
If each task takes 100 milliseconds, the batch takes about 50 seconds instead of about 10.
To get real growth, I'd either use a bounded queue, say an ArrayBlockingQueue of 100, where the same burst gives 2 core threads, 100 queued, 8 extra threads and the rest rejected or run by the caller with CallerRunsPolicy, or more simply set core and max both to 10 with a bounded queue and allowCoreThreadTimeOut so idle threads still go away.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 the order in which ThreadPoolExecutor uses core threads, the queue and extra...": https://scrivio-interview-library.netlify.app/java/executors/b867d779a7/