89 |
|
* ThreadPoolExecutor#getKeepAliveTime}). This provides a means of |
90 |
|
* reducing resource consumption when the pool is not being actively |
91 |
|
* used. If the pool becomes more active later, new threads will be |
92 |
< |
* constructed. This parameter can also be changed dynamically |
93 |
< |
* using method {@link ThreadPoolExecutor#setKeepAliveTime}. Using |
94 |
< |
* a value of <tt>Long.MAX_VALUE</tt> {@link TimeUnit#NANOSECONDS} |
95 |
< |
* effectively disables idle threads from ever terminating prior |
96 |
< |
* to shut down. |
97 |
< |
* </dd> |
92 |
> |
* constructed. This parameter can also be changed dynamically using |
93 |
> |
* method {@link ThreadPoolExecutor#setKeepAliveTime}. Using a value |
94 |
> |
* of <tt>Long.MAX_VALUE</tt> {@link TimeUnit#NANOSECONDS} effectively |
95 |
> |
* disables idle threads from ever terminating prior to shut down. By |
96 |
> |
* default, the keep-alive policy applies only when there are more |
97 |
> |
* than corePoolSizeThreads. But method {@link |
98 |
> |
* ThreadPoolExecutor#allowCoreThreadTimeOut} can be used to apply |
99 |
> |
* this time-out policy to core threads as well. </dd> |
100 |
|
* |
101 |
|
* <dt>Queuing</dt> |
102 |
|
* |
316 |
|
private volatile long keepAliveTime; |
317 |
|
|
318 |
|
/** |
319 |
+ |
* If false (default) core threads stay alive even when idle. |
320 |
+ |
* If true, core threads use keepAliveTime to time out waiting for work. |
321 |
+ |
*/ |
322 |
+ |
private boolean allowCoreThreadTimeOut; |
323 |
+ |
|
324 |
+ |
/** |
325 |
|
* Core pool size, updated only while holding mainLock, |
326 |
|
* but volatile to allow concurrent readability even |
327 |
|
* during updates. |
473 |
|
for (;;) { |
474 |
|
switch(runState) { |
475 |
|
case RUNNING: { |
476 |
< |
if (poolSize <= corePoolSize) // untimed wait if core |
476 |
> |
// untimed wait if core and not allowing core timeout |
477 |
> |
if (poolSize <= corePoolSize && !allowCoreThreadTimeOut) |
478 |
|
return workQueue.take(); |
479 |
|
|
480 |
|
long timeout = keepAliveTime; |
483 |
|
Runnable r = workQueue.poll(timeout, TimeUnit.NANOSECONDS); |
484 |
|
if (r != null) |
485 |
|
return r; |
486 |
< |
if (poolSize > corePoolSize) // timed out |
487 |
< |
return null; |
486 |
> |
if (poolSize > corePoolSize || allowCoreThreadTimeOut) |
487 |
> |
return null; // timed out |
488 |
|
// else, after timeout, pool shrank so shouldn't die, so retry |
489 |
|
break; |
490 |
|
} |
1239 |
|
} |
1240 |
|
|
1241 |
|
/** |
1242 |
+ |
* Returns true if this pool allows core threads to time out and |
1243 |
+ |
* terminate if no tasks arrive within the keepAlive time, being |
1244 |
+ |
* replaced if needed when new tasks arrive. When true, the same |
1245 |
+ |
* keep-alive policy applying to non-core threads applies also to |
1246 |
+ |
* core threads. When false (the default), core threads are never |
1247 |
+ |
* terminated due to lack of incoming tasks. |
1248 |
+ |
* @return <tt>true</tt> if core threads are allowed to time out, |
1249 |
+ |
* else <tt>false</tt> |
1250 |
+ |
*/ |
1251 |
+ |
public boolean allowsCoreThreadTimeOut() { |
1252 |
+ |
return allowCoreThreadTimeOut; |
1253 |
+ |
} |
1254 |
+ |
|
1255 |
+ |
/** |
1256 |
+ |
* Sets the policy governing whether core threads may time out and |
1257 |
+ |
* terminate if no tasks arrive within the keep-alive time, being |
1258 |
+ |
* replaced if needed when new tasks arrive. When false, core |
1259 |
+ |
* threads are never terminated due to lack of incoming |
1260 |
+ |
* tasks. When true, the same keep-alive policy applying to |
1261 |
+ |
* non-core threads applies also to core threads. To avoid |
1262 |
+ |
* continual thread replacement, the keep-alive time must be |
1263 |
+ |
* greater than zero when setting <tt>true</tt>. |
1264 |
+ |
* @param value <tt>true</tt> if should time out, else <tt>false</tt> |
1265 |
+ |
*/ |
1266 |
+ |
public void allowCoreThreadTimeOut(boolean value) { |
1267 |
+ |
allowCoreThreadTimeOut = value; |
1268 |
+ |
} |
1269 |
+ |
|
1270 |
+ |
/** |
1271 |
|
* Sets the maximum allowed number of threads. This overrides any |
1272 |
|
* value set in the constructor. If the new value is smaller than |
1273 |
|
* the current value, excess existing threads will be |