113 |
|
* - the class name of a {@link ForkJoinWorkerThreadFactory} |
114 |
|
* <li>{@code java.util.concurrent.ForkJoinPool.common.exceptionHandler} |
115 |
|
* - the class name of a {@link UncaughtExceptionHandler} |
116 |
+ |
* <!-- |
117 |
+ |
* <li>{@code java.util.concurrent.ForkJoinPool.common.maximumSpares} |
118 |
+ |
* - the maximum number of alloed extra threads to maintain target |
119 |
+ |
* parallelism (default 256). |
120 |
+ |
* --> |
121 |
|
* </ul> |
122 |
|
* If a {@link SecurityManager} is present and no factory is |
123 |
|
* specified, then the default pool uses a factory supplying |
157 |
|
* tasks in other queues. This framework began as vehicle for |
158 |
|
* supporting tree-structured parallelism using work-stealing. |
159 |
|
* Over time, its scalability advantages led to extensions and |
160 |
< |
* changes to better support more diverse usage contexts. |
160 |
> |
* changes to better support more diverse usage contexts. Because |
161 |
> |
* most internal methods and nested classes are interrelated, |
162 |
> |
* their main rationale and descriptions are presented here; |
163 |
> |
* individual methods and nested classes contain only brief |
164 |
> |
* comments about details. |
165 |
|
* |
166 |
|
* WorkQueues |
167 |
|
* ========== |
452 |
|
* |
453 |
|
* Trimming workers. To release resources after periods of lack of |
454 |
|
* use, a worker starting to wait when the pool is quiescent will |
455 |
< |
* time out and terminate if the pool has remained quiescent for a |
456 |
< |
* given period -- a short period if there are more threads than |
457 |
< |
* parallelism, longer as the number of threads decreases. This |
458 |
< |
* eventually terminates all workers after periods of non-use. |
455 |
> |
* time out and terminate (see awaitWork) if the pool has remained |
456 |
> |
* quiescent for period IDLE_TIMEOUT, increasing the period as the |
457 |
> |
* number of threads decreases, eventually removing all workers. |
458 |
> |
* Also, when more than two spare threads exist, excess threads |
459 |
> |
* are immediately terminated at the next quiescent point. |
460 |
> |
* (Padding by two avoids hysteresis). |
461 |
|
* |
462 |
|
* Shutdown and Termination. A call to shutdownNow atomically sets |
463 |
|
* a runState bit and then (non-atomically) sets each worker's |
466 |
|
* termination should commence after a non-abrupt shutdown() call |
467 |
|
* relies on the active count bits of "ctl" maintaining consensus |
468 |
|
* about quiescence. However, external submitters do not take part |
469 |
< |
* in this consensus. So, tryTerminate sweeps through submission |
470 |
< |
* queues to ensure lack of in-flight submissions before |
471 |
< |
* triggering the "STOP" phase of termination. |
469 |
> |
* in this consensus. So, tryTerminate sweeps through queues to |
470 |
> |
* ensure lack of in-flight submissions before triggering the |
471 |
> |
* "STOP" phase of termination. |
472 |
|
* |
473 |
|
* Joining Tasks |
474 |
|
* ============= |
551 |
|
* threads) in the most common case in which it is rarely |
552 |
|
* beneficial: when a worker with an empty queue (thus no |
553 |
|
* continuation tasks) blocks on a join and there still remain |
554 |
< |
* enough threads to ensure liveness. Also, whenever more than two |
544 |
< |
* spare threads are generated, they are killed (see awaitWork) at |
545 |
< |
* the next quiescent point (padding by two avoids hysteresis). |
546 |
< |
* |
547 |
< |
* Bounds. The compensation mechanism is bounded (see MAX_SPARES), |
548 |
< |
* to better enable JVMs to cope with programming errors and abuse |
549 |
< |
* before running out of resources to do so, and may be further |
550 |
< |
* bounded via factories that limit thread construction. The |
551 |
< |
* effects of bounding in this pool (like all others) is |
552 |
< |
* imprecise. Total worker counts are decremented when threads |
553 |
< |
* deregister, not when they exit and resources are reclaimed by |
554 |
< |
* the JVM and OS. So the number of simultaneously live threads |
555 |
< |
* may transiently exceed bounds. |
554 |
> |
* enough threads to ensure liveness. |
555 |
|
* |
556 |
+ |
* Bounds. The compensation mechanism may be bounded. Bounds for |
557 |
+ |
* the commonPool (see commonMaxSpares) better enable JVMs to cope |
558 |
+ |
* with programming errors and abuse before running out of |
559 |
+ |
* resources to do so. In other cases, users may supply factories |
560 |
+ |
* that limit thread construction. The effects of bounding in this |
561 |
+ |
* pool (like all others) is imprecise. Total worker counts are |
562 |
+ |
* decremented when threads deregister, not when they exit and |
563 |
+ |
* resources are reclaimed by the JVM and OS. So the number of |
564 |
+ |
* simultaneously live threads may transiently exceed bounds. |
565 |
|
* |
566 |
|
* Common Pool |
567 |
|
* =========== |
630 |
|
* that help some methods perform reasonably even when interpreted |
631 |
|
* (not compiled). |
632 |
|
* |
633 |
< |
* The order of declarations in this file is: |
633 |
> |
* The order of declarations in this file is (with a few exceptions): |
634 |
|
* (1) Static utility functions |
635 |
|
* (2) Nested (static) classes |
636 |
|
* (3) Static fields |
1223 |
|
static final int commonParallelism; |
1224 |
|
|
1225 |
|
/** |
1226 |
+ |
* Limit on spare thread construction in tryCompensate. |
1227 |
+ |
*/ |
1228 |
+ |
private static int commonMaxSpares; |
1229 |
+ |
|
1230 |
+ |
/** |
1231 |
|
* Sequence number for creating workerNamePrefix. |
1232 |
|
*/ |
1233 |
|
private static int poolNumberSequence; |
1250 |
|
* aggressive shrinkage during most transient stalls (long GCs |
1251 |
|
* etc). |
1252 |
|
*/ |
1253 |
< |
private static final long IDLE_TIMEOUT = 2000L * 1000L * 1000L; // 2sec |
1253 |
> |
private static final long IDLE_TIMEOUT = 2000L * 1000L * 1000L; // 2sec |
1254 |
|
|
1255 |
|
/** |
1256 |
|
* Tolerance for idle timeouts, to cope with timer undershoots |
1257 |
|
*/ |
1258 |
< |
private static final long TIMEOUT_SLOP = 20L * 1000L * 1000L; // 20ms |
1258 |
> |
private static final long TIMEOUT_SLOP = 20L * 1000L * 1000L; // 20ms |
1259 |
|
|
1260 |
|
/** |
1261 |
< |
* Limit on spare thread construction in tryCompensate. The |
1262 |
< |
* current value is far in excess of normal requirements, but also |
1261 |
> |
* The initial value for commonMaxSpares during static |
1262 |
> |
* initialization unless overridden using System property |
1263 |
> |
* "java.util.concurrent.ForkJoinPool.common.maximumSpares". The |
1264 |
> |
* default value is far in excess of normal requirements, but also |
1265 |
|
* far short of MAX_CAP and typical OS thread limits, so allows |
1266 |
|
* JVMs to catch misuse/abuse before running out of resources |
1267 |
|
* needed to do so. |
1268 |
|
*/ |
1269 |
< |
private static int MAX_SPARES = 256; |
1269 |
> |
private static final int DEFAULT_COMMON_MAX_SPARES = 256; |
1270 |
|
|
1271 |
|
/** |
1272 |
|
* Number of times to spin-wait before blocking. The spins (in |
1753 |
|
prevctl = (UC_MASK & (c + AC_UNIT)) | (SP_MASK & pred); |
1754 |
|
int t = (short)(c >>> TC_SHIFT); // shrink excess spares |
1755 |
|
if (t > 2 && U.compareAndSwapLong(this, CTL, c, prevctl)) |
1756 |
< |
return false; |
1756 |
> |
return false; // else use timed wait |
1757 |
|
parkTime = IDLE_TIMEOUT * ((t >= 0) ? 1 : 1 - t); |
1758 |
|
deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP; |
1759 |
|
} |
1916 |
|
* Tries to decrement active count (sometimes implicitly) and |
1917 |
|
* possibly release or create a compensating worker in preparation |
1918 |
|
* for blocking. Returns false (retryable by caller), on |
1919 |
< |
* contention, detected staleness, instability or termination. |
1919 |
> |
* contention, detected staleness, instability, or termination. |
1920 |
|
* |
1921 |
|
* @param w caller |
1922 |
|
*/ |
1948 |
|
(~AC_MASK & c)); // uncompensated |
1949 |
|
canBlock = U.compareAndSwapLong(this, CTL, c, nc); |
1950 |
|
} |
1951 |
< |
else if (tc >= MAX_CAP || tc >= pc + MAX_SPARES) |
1951 |
> |
else if (tc >= MAX_CAP || |
1952 |
> |
(this == common && tc >= pc + commonMaxSpares)) |
1953 |
|
throw new RejectedExecutionException( |
1954 |
|
"Thread limit exceeded replacing blocked worker"); |
1955 |
|
else { // similar to tryAddWorker |
3296 |
|
throw new Error(e); |
3297 |
|
} |
3298 |
|
|
3299 |
+ |
commonMaxSpares = DEFAULT_COMMON_MAX_SPARES; |
3300 |
|
defaultForkJoinWorkerThreadFactory = |
3301 |
|
new DefaultForkJoinWorkerThreadFactory(); |
3302 |
|
modifyThreadPermission = new RuntimePermission("modifyThread"); |
3323 |
|
("java.util.concurrent.ForkJoinPool.common.threadFactory"); |
3324 |
|
String hp = System.getProperty |
3325 |
|
("java.util.concurrent.ForkJoinPool.common.exceptionHandler"); |
3326 |
+ |
String mp = System.getProperty |
3327 |
+ |
("java.util.concurrent.ForkJoinPool.common.maximumSpares"); |
3328 |
|
if (pp != null) |
3329 |
|
parallelism = Integer.parseInt(pp); |
3330 |
|
if (fp != null) |
3333 |
|
if (hp != null) |
3334 |
|
handler = ((UncaughtExceptionHandler)ClassLoader. |
3335 |
|
getSystemClassLoader().loadClass(hp).newInstance()); |
3336 |
+ |
if (mp != null) |
3337 |
+ |
commonMaxSpares = Integer.parseInt(mp); |
3338 |
|
} catch (Exception ignore) { |
3339 |
|
} |
3340 |
|
if (factory == null) { |