Understanding ThreadPoolExecutor keepAliveTime=0 in Java
This article clarifies that setting keepAliveTime to 0 in a Java ThreadPoolExecutor causes excess idle threads beyond corePoolSize to terminate immediately, correcting the common misconception that 0 means threads live forever, and demonstrates the behavior with a concise code example.
In the book "The Art of Concurrency Programming" a passage explains that when the number of threads in a pool exceeds corePoolSize, keepAliveTime is the maximum time that the excess idle threads wait for new tasks before being terminated; setting keepAliveTime to 0L means they are terminated immediately.
Many blog posts incorrectly claim that a keepAliveTime of 0 means idle threads live forever; this article demonstrates the correct behavior with a verification program.
Below is the verification code that creates a ThreadPoolExecutor with corePoolSize 1, maximumPoolSize 2, keepAliveTime 0 seconds, and a queue of size 1. Three tasks are submitted, and the pool size and active thread count are printed every second.
public static void main(String[] args) throws InterruptedException {<br/> // Create a thread pool<br/> ThreadPoolExecutor executor = new ThreadPoolExecutor(1,<br/> 2,<br/> 0, TimeUnit.SECONDS,<br/> new LinkedBlockingQueue<Runnable>(1));<br/><br/> for (int i = 0; i < 3; i++) {<br/> executor.execute(new DemoTask(i));<br/> }<br/><br/> while (true) {<br/> System.out.println("Total threads: " + executor.getPoolSize() + ", Active threads: " + executor.getActiveCount());<br/> TimeUnit.SECONDS.sleep(1);<br/> }<br/> }<br/><br/> static class DemoTask implements Runnable {<br/> int num;<br/><br/> public DemoTask(int i) {<br/> this.num = i;<br/> }<br/><br/> @Override<br/> public void run() {<br/> System.out.println("num=" + num + " Thread = " + Thread.currentThread().getName());<br/> if (num >= 1) {<br/> try {<br/> TimeUnit.SECONDS.sleep(1);<br/> System.out.println("num=" + num + " sleep 1 s finished");<br/> } catch (InterruptedException e) {<br/> e.printStackTrace();<br/> }<br/> } else {<br/> try {<br/> TimeUnit.SECONDS.sleep(3);<br/> System.out.println("num=" + num + " sleep 3 s finished");<br/> } catch (InterruptedException e) {<br/> e.printStackTrace();<br/> }<br/> }<br/> }<br/> }<br/>The printed result shows that after the first task completes, the extra thread (beyond corePoolSize) is terminated immediately, confirming that keepAliveTime=0 does not keep threads alive indefinitely.
This concludes the article.
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.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.
