56 |
|
* maximum number of running threads to 32767. Attempts to create |
57 |
|
* pools with greater than the maximum result in |
58 |
|
* IllegalArgumentExceptions. |
59 |
+ |
* |
60 |
+ |
* @since 1.7 |
61 |
+ |
* @author Doug Lea |
62 |
|
*/ |
63 |
|
public class ForkJoinPool extends AbstractExecutorService { |
64 |
|
|
84 |
|
* Returns a new worker thread operating in the given pool. |
85 |
|
* |
86 |
|
* @param pool the pool this thread works in |
87 |
< |
* @throws NullPointerException if pool is null; |
87 |
> |
* @throws NullPointerException if pool is null |
88 |
|
*/ |
89 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool pool); |
90 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
|
* The uncaught exception handler used when any worker |
159 |
< |
* abrupty terminates |
159 |
> |
* abruptly terminates |
160 |
|
*/ |
161 |
|
private Thread.UncaughtExceptionHandler ueh; |
162 |
|
|
229 |
|
private static int workerCountsFor(int t, int r) { return (t << 16) + r; } |
230 |
|
|
231 |
|
/** |
232 |
< |
* Add delta (which may be negative) to running count. This must |
232 |
> |
* Adds delta (which may be negative) to running count. This must |
233 |
|
* be called before (with negative arg) and after (with positive) |
234 |
< |
* any managed synchronization (i.e., mainly, joins) |
234 |
> |
* any managed synchronization (i.e., mainly, joins). |
235 |
|
* @param delta the number to add |
236 |
|
*/ |
237 |
|
final void updateRunningCount(int delta) { |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
< |
* Add delta (which may be negative) to both total and running |
243 |
> |
* Adds delta (which may be negative) to both total and running |
244 |
|
* count. This must be called upon creation and termination of |
245 |
|
* worker threads. |
246 |
|
* @param delta the number to add |
276 |
|
/** |
277 |
|
* Try incrementing active count; fail on contention. Called by |
278 |
|
* workers before/during executing tasks. |
279 |
< |
* @return true on success; |
279 |
> |
* @return true on success |
280 |
|
*/ |
281 |
|
final boolean tryIncrementActiveCount() { |
282 |
|
int c = runControl; |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* Try decrementing active count; fail on contention. |
288 |
< |
* Possibly trigger termination on success |
287 |
> |
* Tries decrementing active count; fails on contention. |
288 |
> |
* Possibly triggers termination on success. |
289 |
|
* Called by workers when they can't find tasks. |
290 |
|
* @return true on success |
291 |
|
*/ |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
< |
* Return true if argument represents zero active count and |
303 |
> |
* Returns true if argument represents zero active count and |
304 |
|
* nonzero runstate, which is the triggering condition for |
305 |
|
* terminating on shutdown. |
306 |
|
*/ |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* Creates a ForkJoinPool with the indicated parellelism level |
347 |
> |
* Creates a ForkJoinPool with the indicated parallelism level |
348 |
|
* threads, and using the default ForkJoinWorkerThreadFactory, |
349 |
|
* @param parallelism the number of worker threads |
350 |
|
* @throws IllegalArgumentException if parallelism less than or |
379 |
|
* @param parallelism the targeted number of worker threads |
380 |
|
* @param factory the factory for creating new threads |
381 |
|
* @throws IllegalArgumentException if parallelism less than or |
382 |
< |
* equal to zero, or greater than implementation limit. |
382 |
> |
* equal to zero, or greater than implementation limit |
383 |
|
* @throws NullPointerException if factory is null |
384 |
|
* @throws SecurityException if a security manager exists and |
385 |
|
* the caller is not permitted to modify threads |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
< |
* Return a good size for worker array given pool size. |
427 |
> |
* Returns a good size for worker array given pool size. |
428 |
|
* Currently requires size to be a power of two. |
429 |
|
*/ |
430 |
|
private static int arraySizeFor(int ps) { |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
< |
* Create or resize array if necessary to hold newLength. |
436 |
< |
* Call only under exlusion or lock |
435 |
> |
* Creates or resizes array if necessary to hold newLength. |
436 |
> |
* Call only under exclusion or lock. |
437 |
|
* @return the array |
438 |
|
*/ |
439 |
|
private ForkJoinWorkerThread[] ensureWorkerArrayCapacity(int newLength) { |
719 |
|
|
720 |
|
|
721 |
|
/** |
722 |
< |
* Sets the target paralleism level of this pool. |
722 |
> |
* Sets the target parallelism level of this pool. |
723 |
|
* @param parallelism the target parallelism |
724 |
|
* @throws IllegalArgumentException if parallelism less than or |
725 |
< |
* equal to zero or greater than maximum size bounds. |
725 |
> |
* equal to zero or greater than maximum size bounds |
726 |
|
* @throws SecurityException if a security manager exists and |
727 |
|
* the caller is not permitted to modify threads |
728 |
|
* because it does not hold {@link |
785 |
|
* Setting this value has no effect on current pool size. It |
786 |
|
* controls construction of new threads. |
787 |
|
* @throws IllegalArgumentException if negative or greater then |
788 |
< |
* internal implementation limit. |
788 |
> |
* internal implementation limit |
789 |
|
*/ |
790 |
|
public void setMaximumPoolSize(int newMax) { |
791 |
|
if (newMax < 0 || newMax > MAX_THREADS) |
822 |
|
* worker threads only process asynchronous tasks. This method is |
823 |
|
* designed to be invoked only when pool is quiescent, and |
824 |
|
* typically only before any tasks are submitted. The effects of |
825 |
< |
* invocations at ather times may be unpredictable. |
825 |
> |
* invocations at other times may be unpredictable. |
826 |
|
* |
827 |
|
* @param async if true, use locally FIFO scheduling |
828 |
< |
* @return the previous mode. |
828 |
> |
* @return the previous mode |
829 |
|
*/ |
830 |
|
public boolean setAsyncMode(boolean async) { |
831 |
|
boolean oldMode = locallyFifo; |
845 |
|
* Returns true if this pool uses local first-in-first-out |
846 |
|
* scheduling mode for forked tasks that are never joined. |
847 |
|
* |
848 |
< |
* @return true if this pool uses async mode. |
848 |
> |
* @return true if this pool uses async mode |
849 |
|
*/ |
850 |
|
public boolean getAsyncMode() { |
851 |
|
return locallyFifo; |
866 |
|
* Returns an estimate of the number of threads that are currently |
867 |
|
* stealing or executing tasks. This method may overestimate the |
868 |
|
* number of active threads. |
869 |
< |
* @return the number of active threads. |
869 |
> |
* @return the number of active threads |
870 |
|
*/ |
871 |
|
public int getActiveThreadCount() { |
872 |
|
return activeCountOf(runControl); |
876 |
|
* Returns an estimate of the number of threads that are currently |
877 |
|
* idle waiting for tasks. This method may underestimate the |
878 |
|
* number of idle threads. |
879 |
< |
* @return the number of idle threads. |
879 |
> |
* @return the number of idle threads |
880 |
|
*/ |
881 |
|
final int getIdleThreadCount() { |
882 |
|
int c = runningCountOf(workerCounts) - activeCountOf(runControl); |
905 |
|
* tuning fork/join programs: In general, steal counts should be |
906 |
|
* high enough to keep threads busy, but low enough to avoid |
907 |
|
* overhead and contention across threads. |
908 |
< |
* @return the number of steals. |
908 |
> |
* @return the number of steals |
909 |
|
*/ |
910 |
|
public long getStealCount() { |
911 |
|
return stealCount.get(); |
928 |
|
* an approximation, obtained by iterating across all threads in |
929 |
|
* the pool. This method may be useful for tuning task |
930 |
|
* granularities. |
931 |
< |
* @return the number of queued tasks. |
931 |
> |
* @return the number of queued tasks |
932 |
|
*/ |
933 |
|
public long getQueuedTaskCount() { |
934 |
|
long count = 0; |
947 |
|
* Returns an estimate of the number tasks submitted to this pool |
948 |
|
* that have not yet begun executing. This method takes time |
949 |
|
* proportional to the number of submissions. |
950 |
< |
* @return the number of queued submissions. |
950 |
> |
* @return the number of queued submissions |
951 |
|
*/ |
952 |
|
public int getQueuedSubmissionCount() { |
953 |
|
return submissionQueue.size(); |
956 |
|
/** |
957 |
|
* Returns true if there are any tasks submitted to this pool |
958 |
|
* that have not yet begun executing. |
959 |
< |
* @return {@code true} if there are any queued submissions. |
959 |
> |
* @return {@code true} if there are any queued submissions |
960 |
|
*/ |
961 |
|
public boolean hasQueuedSubmissions() { |
962 |
|
return !submissionQueue.isEmpty(); |
976 |
|
* Removes all available unexecuted submitted and forked tasks |
977 |
|
* from scheduling queues and adds them to the given collection, |
978 |
|
* without altering their execution status. These may include |
979 |
< |
* artifically generated or wrapped tasks. This method id designed |
979 |
> |
* artificially generated or wrapped tasks. This method is designed |
980 |
|
* to be invoked only when the pool is known to be |
981 |
|
* quiescent. Invocations at other times may not remove all |
982 |
|
* tasks. A failure encountered while attempting to add elements |
1186 |
|
} |
1187 |
|
|
1188 |
|
/** |
1189 |
< |
* Possibly terminate when on shutdown state |
1189 |
> |
* Possibly terminates when on shutdown state. |
1190 |
|
*/ |
1191 |
|
private void terminateOnShutdown() { |
1192 |
|
if (!hasQueuedSubmissions() && canTerminateOnShutdown(runControl)) |
1194 |
|
} |
1195 |
|
|
1196 |
|
/** |
1197 |
< |
* Clear out and cancel submissions |
1197 |
> |
* Clears out and cancels submissions. |
1198 |
|
*/ |
1199 |
|
private void cancelQueuedSubmissions() { |
1200 |
|
ForkJoinTask<?> task; |
1203 |
|
} |
1204 |
|
|
1205 |
|
/** |
1206 |
< |
* Clean out worker queues. |
1206 |
> |
* Cleans out worker queues. |
1207 |
|
*/ |
1208 |
|
private void cancelQueuedWorkerTasks() { |
1209 |
|
final ReentrantLock lock = this.workerLock; |
1223 |
|
} |
1224 |
|
|
1225 |
|
/** |
1226 |
< |
* Set each worker's status to terminating. Requires lock to avoid |
1227 |
< |
* conflicts with add/remove |
1226 |
> |
* Sets each worker's status to terminating. Requires lock to avoid |
1227 |
> |
* conflicts with add/remove. |
1228 |
|
*/ |
1229 |
|
private void stopAllWorkers() { |
1230 |
|
final ReentrantLock lock = this.workerLock; |
1244 |
|
} |
1245 |
|
|
1246 |
|
/** |
1247 |
< |
* Interrupt all unterminated workers. This is not required for |
1247 |
> |
* Interrupts all unterminated workers. This is not required for |
1248 |
|
* sake of internal control, but may help unstick user code during |
1249 |
|
* shutdown. |
1250 |
|
*/ |
1314 |
|
} |
1315 |
|
|
1316 |
|
/** |
1317 |
< |
* Wake up waiter, returning false if known to already |
1317 |
> |
* Wakes up waiter, returning false if known to already |
1318 |
|
*/ |
1319 |
|
boolean signal() { |
1320 |
|
ForkJoinWorkerThread t = thread; |
1326 |
|
} |
1327 |
|
|
1328 |
|
/** |
1329 |
< |
* Await release on sync |
1329 |
> |
* Awaits release on sync. |
1330 |
|
*/ |
1331 |
|
void awaitSyncRelease(ForkJoinPool p) { |
1332 |
|
while (thread != null && !p.syncIsReleasable(this)) |
1334 |
|
} |
1335 |
|
|
1336 |
|
/** |
1337 |
< |
* Await resumption as spare |
1337 |
> |
* Awaits resumption as spare. |
1338 |
|
*/ |
1339 |
|
void awaitSpareRelease() { |
1340 |
|
while (thread != null) { |
1374 |
|
} |
1375 |
|
|
1376 |
|
/** |
1377 |
< |
* Signal threads waiting to poll a task. Because method sync |
1377 |
> |
* Signals threads waiting to poll a task. Because method sync |
1378 |
|
* rechecks availability, it is OK to only proceed if queue |
1379 |
|
* appears to be non-empty, and OK to skip under contention to |
1380 |
|
* increment count (since some other thread succeeded). |
1461 |
|
// Parallelism maintenance |
1462 |
|
|
1463 |
|
/** |
1464 |
< |
* Decrement running count; if too low, add spare. |
1464 |
> |
* Decrements running count; if too low, adds spare. |
1465 |
|
* |
1466 |
|
* Conceptually, all we need to do here is add or resume a |
1467 |
|
* spare thread when one is about to block (and remove or |
1481 |
|
* only be suspended or removed when they are idle, not |
1482 |
|
* immediately when they aren't needed. So adding threads will |
1483 |
|
* raise parallelism level for longer than necessary. Also, |
1484 |
< |
* FJ applications often enounter highly transient peaks when |
1484 |
> |
* FJ applications often encounter highly transient peaks when |
1485 |
|
* many threads are blocked joining, but for less time than it |
1486 |
|
* takes to create or resume spares. |
1487 |
|
* |
1551 |
|
} |
1552 |
|
|
1553 |
|
/** |
1554 |
< |
* Add a spare worker if lock available and no more than the |
1555 |
< |
* expected numbers of threads exist |
1554 |
> |
* Adds a spare worker if lock available and no more than the |
1555 |
> |
* expected numbers of threads exist. |
1556 |
|
* @return true if successful |
1557 |
|
*/ |
1558 |
|
private boolean tryAddSpare(int expectedCounts) { |
1585 |
|
} |
1586 |
|
|
1587 |
|
/** |
1588 |
< |
* Add the kth spare worker. On entry, pool coounts are already |
1588 |
> |
* Adds the kth spare worker. On entry, pool counts are already |
1589 |
|
* adjusted to reflect addition. |
1590 |
|
*/ |
1591 |
|
private void createAndStartSpare(int k) { |
1607 |
|
} |
1608 |
|
|
1609 |
|
/** |
1610 |
< |
* Suspend calling thread w if there are excess threads. Called |
1611 |
< |
* only from sync. Spares are enqueued in a Treiber stack |
1612 |
< |
* using the same WaitQueueNodes as barriers. They are resumed |
1613 |
< |
* mainly in preJoin, but are also woken on pool events that |
1614 |
< |
* require all threads to check run state. |
1610 |
> |
* Suspends calling thread w if there are excess threads. Called |
1611 |
> |
* only from sync. Spares are enqueued in a Treiber stack using |
1612 |
> |
* the same WaitQueueNodes as barriers. They are resumed mainly |
1613 |
> |
* in preJoin, but are also woken on pool events that require all |
1614 |
> |
* threads to check run state. |
1615 |
|
* @param w the caller |
1616 |
|
*/ |
1617 |
|
private boolean suspendIfSpare(ForkJoinWorkerThread w) { |
1632 |
|
} |
1633 |
|
|
1634 |
|
/** |
1635 |
< |
* Try to pop and resume a spare thread. |
1635 |
> |
* Tries to pop and resume a spare thread. |
1636 |
|
* @param updateCount if true, increment running count on success |
1637 |
|
* @return true if successful |
1638 |
|
*/ |
1650 |
|
} |
1651 |
|
|
1652 |
|
/** |
1653 |
< |
* Pop and resume all spare threads. Same idea as ensureSync. |
1653 |
> |
* Pops and resumes all spare threads. Same idea as ensureSync. |
1654 |
|
* @return true if any spares released |
1655 |
|
*/ |
1656 |
|
private boolean resumeAllSpares() { |
1668 |
|
} |
1669 |
|
|
1670 |
|
/** |
1671 |
< |
* Pop and shutdown excessive spare threads. Call only while |
1671 |
> |
* Pops and shuts down excessive spare threads. Call only while |
1672 |
|
* holding lock. This is not guaranteed to eliminate all excess |
1673 |
|
* threads, only those suspended as spares, which are the ones |
1674 |
|
* unlikely to be needed in the future. |
1720 |
|
* Possibly blocks the current thread, for example waiting for |
1721 |
|
* a lock or condition. |
1722 |
|
* @return true if no additional blocking is necessary (i.e., |
1723 |
< |
* if isReleasable would return true). |
1723 |
> |
* if isReleasable would return true) |
1724 |
|
* @throws InterruptedException if interrupted while waiting |
1725 |
< |
* (the method is not required to do so, but is allowe to). |
1725 |
> |
* (the method is not required to do so, but is allowed to). |
1726 |
|
*/ |
1727 |
|
boolean block() throws InterruptedException; |
1728 |
|
|
1759 |
|
* attempt to maintain the pool's nominal parallelism; otherwise |
1760 |
|
* activate a thread only if necessary to avoid complete |
1761 |
|
* starvation. |
1762 |
< |
* @throws InterruptedException if blocker.block did so. |
1762 |
> |
* @throws InterruptedException if blocker.block did so |
1763 |
|
*/ |
1764 |
|
public static void managedBlock(ManagedBlocker blocker, |
1765 |
|
boolean maintainParallelism) |
1821 |
|
|
1822 |
|
private static long fieldOffset(String fieldName) |
1823 |
|
throws NoSuchFieldException { |
1824 |
< |
return _unsafe.objectFieldOffset |
1824 |
> |
return UNSAFE.objectFieldOffset |
1825 |
|
(ForkJoinPool.class.getDeclaredField(fieldName)); |
1826 |
|
} |
1827 |
|
|
1828 |
< |
static final Unsafe _unsafe; |
1828 |
> |
static final Unsafe UNSAFE; |
1829 |
|
static final long eventCountOffset; |
1830 |
|
static final long workerCountsOffset; |
1831 |
|
static final long runControlOffset; |
1834 |
|
|
1835 |
|
static { |
1836 |
|
try { |
1837 |
< |
_unsafe = getUnsafe(); |
1837 |
> |
UNSAFE = getUnsafe(); |
1838 |
|
eventCountOffset = fieldOffset("eventCount"); |
1839 |
|
workerCountsOffset = fieldOffset("workerCounts"); |
1840 |
|
runControlOffset = fieldOffset("runControl"); |
1846 |
|
} |
1847 |
|
|
1848 |
|
private boolean casEventCount(long cmp, long val) { |
1849 |
< |
return _unsafe.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1849 |
> |
return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1850 |
|
} |
1851 |
|
private boolean casWorkerCounts(int cmp, int val) { |
1852 |
< |
return _unsafe.compareAndSwapInt(this, workerCountsOffset, cmp, val); |
1852 |
> |
return UNSAFE.compareAndSwapInt(this, workerCountsOffset, cmp, val); |
1853 |
|
} |
1854 |
|
private boolean casRunControl(int cmp, int val) { |
1855 |
< |
return _unsafe.compareAndSwapInt(this, runControlOffset, cmp, val); |
1855 |
> |
return UNSAFE.compareAndSwapInt(this, runControlOffset, cmp, val); |
1856 |
|
} |
1857 |
|
private boolean casSpareStack(WaitQueueNode cmp, WaitQueueNode val) { |
1858 |
< |
return _unsafe.compareAndSwapObject(this, spareStackOffset, cmp, val); |
1858 |
> |
return UNSAFE.compareAndSwapObject(this, spareStackOffset, cmp, val); |
1859 |
|
} |
1860 |
|
private boolean casBarrierStack(WaitQueueNode cmp, WaitQueueNode val) { |
1861 |
< |
return _unsafe.compareAndSwapObject(this, syncStackOffset, cmp, val); |
1861 |
> |
return UNSAFE.compareAndSwapObject(this, syncStackOffset, cmp, val); |
1862 |
|
} |
1863 |
|
} |