Quick revision
The short answer
keepAliveTime is how long a thread above the core size may stay idle before it ends. allowCoreThreadTimeOut(true) applies the same timeout to core threads, so an idle pool can shrink to zero, and prestartAllCoreThreads() creates all core threads up front so the first requests do not pay for thread creation. Tomcat changes the default queue-before-grow order with its own task queue, which refuses an offer while the pool is below its maximum, so the executor creates threads before it queues tasks; we can build the same effect with a custom queue whose offer returns false while growth is still possible.
What to remember
In this answer, we saw that keepAliveTime ends idle threads above the core size, allowCoreThreadTimeOut(true) applies that timeout to core threads so an idle pool can reach zero, and prestartAllCoreThreads creates core threads up front to avoid first-request latency. Tomcat changes the default queue-before-grow order with a task queue that refuses offers while the pool is below its maximum and no thread is idle, so the executor adds threads first and queues only at the maximum. We built the same effect with a LinkedTransferQueue whose offer uses tryTransfer and a rejection handler that forces the task into the queue.
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.
keepAliveTime is how long a thread above the core size may stay idle before it ends. allowCoreThreadTimeOut(true) applies the same timeout to core threads, so an idle pool can shrink to zero, and prestartAllCoreThreads() creates all core threads up front so the first requests do not pay for thread creation. Tomcat changes the default queue-before-grow order with its own task queue, which refuses an offer while the pool is below its maximum, so the executor creates threads before it queues tasks; we can build the same effect with a custom queue whose offer returns false while growth is still possible.
Background to this question
The standard ThreadPoolExecutor was designed for batch-style work: keep a few threads, queue the rest. A web server needs the opposite: respond to a burst by adding threads quickly, and only queue once it cannot add more. Understanding the three lifecycle settings and how Tomcat bends the growth order explains a lot of everyday configuration, from server.tomcat.threads.max to custom executors for I/O-heavy work. In this answer, we will recap the growth order, go through keepAliveTime, allowCoreThreadTimeOut and prestartAllCoreThreads with examples, see how Tomcat's queue makes the pool grow first, and build the same behaviour ourselves.
1A Quick Recap of the Default Growth Order
When a task arrives, ThreadPoolExecutor creates a thread if it has fewer than corePoolSize threads, otherwise offers the task to the queue, and only if the queue refuses creates extra threads up to maximumPoolSize. So the pool grows beyond the core only when the queue is full. With a large queue, extra threads appear late or never, and tasks wait even though the pool is allowed to have more threads. Keep this order in mind; everything below either tunes the life of threads within it or changes it.
2KeepAliveTime: When Extra Threads End
Threads beyond the core size are meant to be temporary. keepAliveTime says how long such a thread may wait for a new task before it ends. Technically, an extra thread asks the queue for work with poll(keepAliveTime); if nothing arrives in that time and the pool has more than core threads, the thread exits. Core threads, by contrast, wait with take(), which never times out, so they stay forever by default.
3AllowCoreThreadTimeOut: Letting the Pool Shrink to Zero
allowCoreThreadTimeOut(true) makes core threads follow the same rule, so after keepAliveTime with no work, even they end and an idle pool can shrink to zero threads. This suits pools that are busy only occasionally, such as a nightly export pool in a long-running service, where keeping threads alive all day wastes memory. The keep-alive time must be greater than zero when this is switched on, otherwise the method throws IllegalArgumentException.
It also enables a useful configuration trick. Setting core and maximum to the same value, with a bounded queue and allowCoreThreadTimeOut(true), gives a pool that creates threads up to that number for new work, then queues, and shrinks when idle. That is close to the behaviour people expect from a pool, without any custom code. The one difference is that while below the core size, a new task starts a new thread even if another thread is idle.
4PrestartAllCoreThreads: Paying the Cost Up Front
Normally core threads are created lazily, one per task, as the first tasks arrive. The first requests after startup therefore pay for thread creation, and for any per-thread warm-up such as initialising thread-local caches. prestartAllCoreThreads() creates all core threads immediately and returns how many it started; prestartCoreThread() starts just one. This is useful for latency-sensitive services that receive traffic right after a deploy, and it is pointless when combined with allowCoreThreadTimeOut and a long idle period, because the threads will simply time out again.
5How Tomcat Makes the Pool Grow First
Tomcat's request executor needs the opposite of the default order: when a burst arrives, it should add threads up to its maximum, 200 by default in Spring Boot through server.tomcat.threads.max, and only queue once all of them are busy. Tomcat achieves this with its own executor class and a task queue, TaskQueue in org.apache.tomcat.util.threads, whose offer method looks at the executor before accepting. If the pool is already at its maximum, the task is queued. If there are at least as many threads as submitted tasks, meaning some thread is idle, the task is queued so the idle thread picks it up. Otherwise, if the pool is below its maximum, offer returns false.
Returning false is the clever part. The executor interprets a refused offer as 'the queue is full', which by its own rules means 'create a new thread'. So Tomcat reuses the standard growth logic but tricks it into growing first. When the pool is at its maximum and a task is still refused, Tomcat's executor catches the rejection and forces the task into the queue. The exact code differs between Tomcat versions, so check your version for the details, but the idea has stayed the same for many years. In Spring Boot, server.tomcat.threads.min-spare sets the number of threads kept ready, and server.tomcat.accept-count limits connections waiting at the socket level.
- Pool already at max threads?YesQueue the taskNoNext check
- An idle thread available?YesQueue it for that threadNoNext check
- Below max threads?YesReturn false: executor adds a threadNoQueue the task
3 steps in order. Play them, or step through them one at a time.
A refused offer makes the executor create a thread, so Tomcat grows to its maximum before queuing.
6Building a Grow-First Pool Ourselves
We can get the same behaviour with a standard ThreadPoolExecutor and a small custom queue. LinkedTransferQueue has a method tryTransfer(e) that hands an element directly to a thread already waiting in take() or poll(), and returns false if no thread is waiting. If we make offer use tryTransfer, the queue accepts a task only when an idle thread exists, and otherwise says 'full', so the executor adds a thread. When the pool is at its maximum, the executor rejects the task, and our rejection handler puts it into the queue for real.
Notice two details. The forced queue is unbounded, so if we need a limit we check queue.size() in the handler and reject beyond it. And core size should be at least 1, so that a task forced into the queue always has a thread that will eventually take it. A small race, a thread becoming idle just after tryTransfer fails, only means one extra thread is created, which the keep-alive time later removes.
7Follow-up questions
Does Tomcat use this executor when virtual threads are enabled?
No. With spring.threads.virtual.enabled=true on Spring Boot 3.2 and later, Tomcat runs each request on a new virtual thread, so the thread maximum and the grow-first queue no longer apply. Limits on concurrent work then come from connection limits and downstream pools.
Why not just use a SynchronousQueue to grow first?
A SynchronousQueue does grow threads first, because its offer fails unless a thread is waiting, but it has no buffer at all. Once the pool reaches its maximum, every further task is rejected immediately. The Tomcat approach adds a queue behind the maximum.
Can keepAliveTime be changed at run time?
Yes. setKeepAliveTime, setCorePoolSize and setMaximumPoolSize all take effect on a running ThreadPoolExecutor, which is how some services adjust pools from configuration without a restart.
8Conclusion
In this answer, we saw that keepAliveTime ends idle threads above the core size, allowCoreThreadTimeOut(true) applies that timeout to core threads so an idle pool can reach zero, and prestartAllCoreThreads creates core threads up front to avoid first-request latency. Tomcat changes the default queue-before-grow order with a task queue that refuses offers while the pool is below its maximum and no thread is idle, so the executor adds threads first and queues only at the maximum. We built the same effect with a LinkedTransferQueue whose offer uses tryTransfer and a rejection handler that forces the task into the queue.
Open with
keepAliveTime is how long a thread above the core size can sit idle before it exits; technically extra threads poll the queue with that timeout, while core threads block in take forever. allowCoreThreadTimeOut true applies the timeout to core threads too, so an idle pool can shrink to zero, which is nice for occasional work.
Then, if they want more
Setting core equal to max with a bounded queue and core timeout is also a simple way to get up to N threads, then queue, and shrink when idle. prestartAllCoreThreads creates all the core threads immediately, so the first requests after startup don't pay for thread creation.
Tomcat needs the opposite of the default growth order, because a web server should add threads during a burst before queuing.
The default executor only grows past the core when the queue refuses an offer, so Tomcat's TaskQueue refuses offers on purpose: if the pool is below its maximum and no thread is idle, offer returns false, and the executor reacts by creating a thread.
When it's at the max, the task is queued, and if it's rejected anyway, Tomcat forces it into the queue.
I can build the same thing with a LinkedTransferQueue subclass whose offer calls tryTransfer, which only succeeds if an idle thread is waiting, plus a rejection handler that does the real enqueue once the pool is at its maximum, ideally with a size check so the queue stays bounded.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.2
- 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 "What do allowCoreThreadTimeOut, prestartAllCoreThreads and keepAliveTime do, and how...": https://scrivio-interview-library.netlify.app/java/executors/cc8a37a73d/