70

In continuation to a question posted by me, I'm trying to use ThreadPoolExecutor in my codebase. Even after repeated attempts to comprehend from Java API doc, I failed to understand clearly the functionality/purpose behind keepAliveTime parameter to be passed in the constructor. Hope somebody can explain me with some good working example.

Excerpts from Java doc:

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue)

keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

Community
  • 1
  • 1
Gnanam
  • 10,613
  • 19
  • 54
  • 72
  • 2
    Just as a point of terminology, you've twice used the word "implement" when I think you just mean "use". You're not trying to *implement* `ThreadPoolExecutor` by writing your own code implementing that API - you're just creating a `ThreadPoolExecutor` and you want to know how it will behave, right? – Jon Skeet Apr 30 '12 at 06:23
  • Yes, you're right. I just used to mean that it is a new kind of implementation in my codebase. – Gnanam Apr 30 '12 at 06:44
  • But it's not an implementation *of* `ThreadPoolExecutor`. It's really helpful if you can use terminology appropriately - *particularly* on Stack Overflow. – Jon Skeet Apr 30 '12 at 06:45

3 Answers3

105

Suppose you have a core size of 5, and a maximum size of 15. For some reason your pool gets busy, and uses all 15 available threads. Eventually you run out of work to do - so some of your threads become idle as they finish their final task. So 10 of those threads are allowed to die.

However, to avoid them being killed off too quickly, you can specify the keep-alive time. So if you specified 1 as the keepAliveTime value and TimeUnit.MINUTE as the unit value, each thread would wait one minute after it had finished executing a task to see if there was more work to do. If it still hadn't been given any more work, it would let itself complete, until there were only 5 threads in the pool - the "core" of the pool.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    If I understood this correctly, `keepAliveTime` is used/effective "only" when threads in the pool has more than core size. This is *not* applicable when threads in the pool is below core size. – Gnanam Apr 30 '12 at 07:03
  • Sorry, if this question is too basic. But why do we want to keep threads alive which has really finished executing a task? Anyhow, maximum number of threads (`maximumPoolSize`) allowed in the pool is limited right? – Gnanam Apr 30 '12 at 07:18
  • 3
    The point of killing threads is the same as the point of having a thread pool in the first place -- not wasting unnecessary resources. – Marko Topolnik Apr 30 '12 at 07:54
  • 6
    @Gnanam - so that the threads can execute another task, so saving on the overhead of creating a new thread. – Martin James Apr 30 '12 at 08:15
  • @MartinJames & @Marko: Thanks for the clarification. So that means, about my other question - `keepAliveTime` parameter makes sense "only" when threads in the pool has more than core size -- Is my understanding right? – Gnanam Apr 30 '12 at 09:56
  • 2
    @Gnanam: That's certainly my understanding too. That's not to say it's *necessarily* correct, but it would at least be consistent :) – Jon Skeet Apr 30 '12 at 09:59
  • @JonSkeet. What does it actually mean when we thread is idle. Lets say i have created 10 threads. The task submitted to the thread only contains one network call, will the thread waiting for the network call be considered idle. – Prabhath Mar 28 '16 at 10:36
  • 2
    @Prabhath: Assuming by "waiting for the network call" you mean it's making a network call and blocking for it to be finished, then no, it's not idle - it's block. If it were idle, it could be given another task. If it's blocked, it can't be. – Jon Skeet Mar 28 '16 at 17:21
  • @JonSkeet Thanks that's what i was asking – Prabhath Mar 29 '16 at 08:34
  • @user_x: Is there anything that makes you think it doesn't? – Jon Skeet May 18 '16 at 11:43
  • 2
    @Gnanam It also can be applied to the core threads. From the docs: `By default, the keep-alive policy applies only when there are more than corePoolSize threads. But method {@link #allowCoreThreadTimeOut(boolean)} can be used to apply this time-out policy to core threads as well, so long as the keepAliveTime value is non-zero.` – Zaheer Nov 07 '16 at 05:00
  • 2
    I was also interested in how threads in a pool a stored/deleted if `keepAliveTime` is 0. The answer is [here](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#setKeepAliveTime(long,%20java.util.concurrent.TimeUnit)): a time value of zero will cause excess threads to terminate immediately after executing tasks. And 0 is used in [newFixedThreadPool](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int)) – Maksim Dmitriev Jan 07 '17 at 19:21
6

Here is some more description from the Javadoc:

<dt>Keep-alive times</dt>
 *
 * <dd>If the pool currently has more than corePoolSize threads,
 * excess threads will be terminated if they have been idle for more
 * than the keepAliveTime (see {@link
 * ThreadPoolExecutor#getKeepAliveTime}). This provides a means of
 * reducing resource consumption when the pool is not being actively
 * used. If the pool becomes more active later, new threads will be
 * constructed. This parameter can also be changed dynamically
 * using method {@link ThreadPoolExecutor#setKeepAliveTime}. Using
 * a value of <tt>Long.MAX_VALUE</tt> {@link TimeUnit#NANOSECONDS}
 * effectively disables idle threads from ever terminating prior
 * to shut down.
 * </dd>
 *

Essentially this just allows you to control the number of threads left in the idle pool. If you make this too small (for what you are doing), you will be creating too many threads. If you make it too large, you will be consuming memory/threads you don't need to.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
0

Here is example of code, which demonstrates work of keepAliveTime How does maximumPoolSize of ThreadPoolExecutor works?

Community
  • 1
  • 1
Dmytro Kryvenko
  • 343
  • 4
  • 16