23 |
|
* An {@link ExecutorService} for running {@link ForkJoinTask}s. |
24 |
|
* A {@code ForkJoinPool} provides the entry point for submissions |
25 |
|
* from non-{@code ForkJoinTask}s, as well as management and |
26 |
< |
* monitoring operations. Normally a single {@code ForkJoinPool} is |
27 |
< |
* used for a large number of submitted tasks. Otherwise, use would |
28 |
< |
* not usually outweigh the construction and bookkeeping overhead of |
29 |
< |
* creating a large set of threads. |
26 |
> |
* monitoring operations. |
27 |
|
* |
28 |
< |
* <p>{@code ForkJoinPool}s differ from other kinds of {@link |
29 |
< |
* Executor}s mainly in that they provide <em>work-stealing</em>: all |
30 |
< |
* threads in the pool attempt to find and execute subtasks created by |
31 |
< |
* other active tasks (eventually blocking if none exist). This makes |
32 |
< |
* them efficient when most tasks spawn other subtasks (as do most |
33 |
< |
* {@code ForkJoinTask}s), as well as the mixed execution of some |
34 |
< |
* plain {@code Runnable}- or {@code Callable}- based activities along |
35 |
< |
* with {@code ForkJoinTask}s. When setting {@linkplain #setAsyncMode |
36 |
< |
* async mode}, a {@code ForkJoinPool} may also be appropriate for use |
37 |
< |
* with fine-grained tasks that are never joined. Otherwise, other |
38 |
< |
* {@code ExecutorService} implementations are typically more |
39 |
< |
* appropriate choices. |
28 |
> |
* <p>A {@code ForkJoinPool} differs from other kinds of {@link |
29 |
> |
* ExecutorService} mainly by virtue of employing |
30 |
> |
* <em>work-stealing</em>: all threads in the pool attempt to find and |
31 |
> |
* execute subtasks created by other active tasks (eventually blocking |
32 |
> |
* waiting for work if none exist). This enables efficient processing |
33 |
> |
* when most tasks spawn other subtasks (as do most {@code |
34 |
> |
* ForkJoinTask}s). A {@code ForkJoinPool} may also be used for mixed |
35 |
> |
* execution of some plain {@code Runnable}- or {@code Callable}- |
36 |
> |
* based activities along with {@code ForkJoinTask}s. When setting |
37 |
> |
* {@linkplain #setAsyncMode async mode}, a {@code ForkJoinPool} may |
38 |
> |
* also be appropriate for use with fine-grained tasks of any form |
39 |
> |
* that are never joined. Otherwise, other {@code ExecutorService} |
40 |
> |
* implementations are typically more appropriate choices. |
41 |
|
* |
42 |
< |
* <p>A {@code ForkJoinPool} may be constructed with a given |
43 |
< |
* parallelism level (target pool size), which it attempts to maintain |
44 |
< |
* by dynamically adding, suspending, or resuming threads, even if |
45 |
< |
* some tasks are waiting to join others. However, no such adjustments |
42 |
> |
* <p>A {@code ForkJoinPool} is constructed with a given target |
43 |
> |
* parallelism level; by default, equal to the number of available |
44 |
> |
* processors. Unless configured otherwise via {@link |
45 |
> |
* #setMaintainsParallelism}, the pool attempts to maintain this |
46 |
> |
* number of active (or available) threads by dynamically adding, |
47 |
> |
* suspending, or resuming internal worker threads, even if some tasks |
48 |
> |
* are stalled waiting to join others. However, no such adjustments |
49 |
|
* are performed in the face of blocked IO or other unmanaged |
50 |
|
* synchronization. The nested {@link ManagedBlocker} interface |
51 |
|
* enables extension of the kinds of synchronization accommodated. |
52 |
|
* The target parallelism level may also be changed dynamically |
53 |
< |
* ({@link #setParallelism}) and thread construction can be limited |
54 |
< |
* using methods {@link #setMaximumPoolSize} and/or {@link |
55 |
< |
* #setMaintainsParallelism}. |
53 |
> |
* ({@link #setParallelism}). The total number of threads may be |
54 |
> |
* limited using method {@link #setMaximumPoolSize}, in which case it |
55 |
> |
* may become possible for the activities of a pool to stall due to |
56 |
> |
* the lack of available threads to process new tasks. |
57 |
|
* |
58 |
|
* <p>In addition to execution and lifecycle control methods, this |
59 |
|
* class provides status check methods (for example |
62 |
|
* {@link #toString} returns indications of pool state in a |
63 |
|
* convenient form for informal monitoring. |
64 |
|
* |
65 |
+ |
* <p><b>Sample Usage.</b> Normally a single {@code ForkJoinPool} is |
66 |
+ |
* used for all parallel task execution in a program or subsystem. |
67 |
+ |
* Otherwise, use would not usually outweigh the construction and |
68 |
+ |
* bookkeeping overhead of creating a large set of threads. For |
69 |
+ |
* example, a common pool could be used for the {@code SortTasks} |
70 |
+ |
* illustrated in {@link RecursiveAction}. Because {@code |
71 |
+ |
* ForkJoinPool} uses threads in {@linkplain java.lang.Thread#isDaemon |
72 |
+ |
* daemon} mode, there is typically no need to explicitly {@link |
73 |
+ |
* #shutdown} such a pool upon program exit. |
74 |
+ |
* |
75 |
+ |
* <pre> |
76 |
+ |
* static final ForkJoinPool mainPool = new ForkJoinPool(); |
77 |
+ |
* ... |
78 |
+ |
* public void sort(long[] array) { |
79 |
+ |
* mainPool.invoke(new SortTask(array, 0, array.length)); |
80 |
+ |
* } |
81 |
+ |
* </pre> |
82 |
+ |
* |
83 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
84 |
|
* maximum number of running threads to 32767. Attempts to create |
85 |
< |
* pools with greater than the maximum result in |
85 |
> |
* pools with greater than the maximum number result in |
86 |
|
* {@code IllegalArgumentException}. |
87 |
|
* |
88 |
+ |
* <p>This implementation rejects submitted tasks (that is, by throwing |
89 |
+ |
* {@link RejectedExecutionException}) only when the pool is shut down. |
90 |
+ |
* |
91 |
|
* @since 1.7 |
92 |
|
* @author Doug Lea |
93 |
|
*/ |
115 |
|
* Returns a new worker thread operating in the given pool. |
116 |
|
* |
117 |
|
* @param pool the pool this thread works in |
118 |
< |
* @throws NullPointerException if pool is null |
118 |
> |
* @throws NullPointerException if the pool is null |
119 |
|
*/ |
120 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool pool); |
121 |
|
} |
366 |
|
// Constructors |
367 |
|
|
368 |
|
/** |
369 |
< |
* Creates a ForkJoinPool with a pool size equal to the number of |
370 |
< |
* processors available on the system, using the default |
371 |
< |
* ForkJoinWorkerThreadFactory. |
369 |
> |
* Creates a {@code ForkJoinPool} with parallelism equal to {@link |
370 |
> |
* java.lang.Runtime#availableProcessors}, and using the {@linkplain |
371 |
> |
* #defaultForkJoinWorkerThreadFactory default thread factory}. |
372 |
|
* |
373 |
|
* @throws SecurityException if a security manager exists and |
374 |
|
* the caller is not permitted to modify threads |
381 |
|
} |
382 |
|
|
383 |
|
/** |
384 |
< |
* Creates a ForkJoinPool with the indicated parallelism level |
385 |
< |
* threads and using the default ForkJoinWorkerThreadFactory. |
384 |
> |
* Creates a {@code ForkJoinPool} with the indicated parallelism |
385 |
> |
* level and using the {@linkplain |
386 |
> |
* #defaultForkJoinWorkerThreadFactory default thread factory}. |
387 |
|
* |
388 |
< |
* @param parallelism the number of worker threads |
388 |
> |
* @param parallelism the parallelism level |
389 |
|
* @throws IllegalArgumentException if parallelism less than or |
390 |
< |
* equal to zero |
390 |
> |
* equal to zero, or greater than implementation limit |
391 |
|
* @throws SecurityException if a security manager exists and |
392 |
|
* the caller is not permitted to modify threads |
393 |
|
* because it does not hold {@link |
398 |
|
} |
399 |
|
|
400 |
|
/** |
401 |
< |
* Creates a ForkJoinPool with parallelism equal to the number of |
402 |
< |
* processors available on the system and using the given |
403 |
< |
* ForkJoinWorkerThreadFactory. |
401 |
> |
* Creates a {@code ForkJoinPool} with parallelism equal to {@link |
402 |
> |
* java.lang.Runtime#availableProcessors}, and using the given |
403 |
> |
* thread factory. |
404 |
|
* |
405 |
|
* @param factory the factory for creating new threads |
406 |
< |
* @throws NullPointerException if factory is null |
406 |
> |
* @throws NullPointerException if the factory is null |
407 |
|
* @throws SecurityException if a security manager exists and |
408 |
|
* the caller is not permitted to modify threads |
409 |
|
* because it does not hold {@link |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
< |
* Creates a ForkJoinPool with the given parallelism and factory. |
417 |
> |
* Creates a {@code ForkJoinPool} with the given parallelism and |
418 |
> |
* thread factory. |
419 |
|
* |
420 |
< |
* @param parallelism the targeted number of worker threads |
420 |
> |
* @param parallelism the parallelism level |
421 |
|
* @param factory the factory for creating new threads |
422 |
|
* @throws IllegalArgumentException if parallelism less than or |
423 |
< |
* equal to zero, or greater than implementation limit |
424 |
< |
* @throws NullPointerException if factory is null |
423 |
> |
* equal to zero, or greater than implementation limit |
424 |
> |
* @throws NullPointerException if the factory is null |
425 |
|
* @throws SecurityException if a security manager exists and |
426 |
|
* the caller is not permitted to modify threads |
427 |
|
* because it does not hold {@link |
449 |
|
* Creates a new worker thread using factory. |
450 |
|
* |
451 |
|
* @param index the index to assign worker |
452 |
< |
* @return new worker, or null of factory failed |
452 |
> |
* @return new worker, or null if factory failed |
453 |
|
*/ |
454 |
|
private ForkJoinWorkerThread createWorker(int index) { |
455 |
|
Thread.UncaughtExceptionHandler h = ueh; |
470 |
|
* Currently requires size to be a power of two. |
471 |
|
*/ |
472 |
|
private static int arraySizeFor(int poolSize) { |
473 |
< |
return (poolSize <= 1) ? 1 : |
474 |
< |
(1 << (32 - Integer.numberOfLeadingZeros(poolSize-1))); |
473 |
> |
if (poolSize <= 1) |
474 |
> |
return 1; |
475 |
> |
// See Hackers Delight, sec 3.2 |
476 |
> |
int c = poolSize >= MAX_THREADS ? MAX_THREADS : (poolSize - 1); |
477 |
> |
c |= c >>> 1; |
478 |
> |
c |= c >>> 2; |
479 |
> |
c |= c >>> 4; |
480 |
> |
c |= c >>> 8; |
481 |
> |
c |= c >>> 16; |
482 |
> |
return c + 1; |
483 |
|
} |
484 |
|
|
485 |
|
/** |
597 |
|
* |
598 |
|
* @param task the task |
599 |
|
* @return the task's result |
600 |
< |
* @throws NullPointerException if task is null |
601 |
< |
* @throws RejectedExecutionException if pool is shut down |
600 |
> |
* @throws NullPointerException if the task is null |
601 |
> |
* @throws RejectedExecutionException if the task cannot be |
602 |
> |
* scheduled for execution |
603 |
|
*/ |
604 |
|
public <T> T invoke(ForkJoinTask<T> task) { |
605 |
|
doSubmit(task); |
610 |
|
* Arranges for (asynchronous) execution of the given task. |
611 |
|
* |
612 |
|
* @param task the task |
613 |
< |
* @throws NullPointerException if task is null |
614 |
< |
* @throws RejectedExecutionException if pool is shut down |
613 |
> |
* @throws NullPointerException if the task is null |
614 |
> |
* @throws RejectedExecutionException if the task cannot be |
615 |
> |
* scheduled for execution |
616 |
|
*/ |
617 |
|
public void execute(ForkJoinTask<?> task) { |
618 |
|
doSubmit(task); |
620 |
|
|
621 |
|
// AbstractExecutorService methods |
622 |
|
|
623 |
+ |
/** |
624 |
+ |
* @throws NullPointerException if the task is null |
625 |
+ |
* @throws RejectedExecutionException if the task cannot be |
626 |
+ |
* scheduled for execution |
627 |
+ |
*/ |
628 |
|
public void execute(Runnable task) { |
629 |
|
ForkJoinTask<?> job; |
630 |
|
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
634 |
|
doSubmit(job); |
635 |
|
} |
636 |
|
|
637 |
+ |
/** |
638 |
+ |
* @throws NullPointerException if the task is null |
639 |
+ |
* @throws RejectedExecutionException if the task cannot be |
640 |
+ |
* scheduled for execution |
641 |
+ |
*/ |
642 |
|
public <T> ForkJoinTask<T> submit(Callable<T> task) { |
643 |
|
ForkJoinTask<T> job = ForkJoinTask.adapt(task); |
644 |
|
doSubmit(job); |
645 |
|
return job; |
646 |
|
} |
647 |
|
|
648 |
+ |
/** |
649 |
+ |
* @throws NullPointerException if the task is null |
650 |
+ |
* @throws RejectedExecutionException if the task cannot be |
651 |
+ |
* scheduled for execution |
652 |
+ |
*/ |
653 |
|
public <T> ForkJoinTask<T> submit(Runnable task, T result) { |
654 |
|
ForkJoinTask<T> job = ForkJoinTask.adapt(task, result); |
655 |
|
doSubmit(job); |
656 |
|
return job; |
657 |
|
} |
658 |
|
|
659 |
+ |
/** |
660 |
+ |
* @throws NullPointerException if the task is null |
661 |
+ |
* @throws RejectedExecutionException if the task cannot be |
662 |
+ |
* scheduled for execution |
663 |
+ |
*/ |
664 |
|
public ForkJoinTask<?> submit(Runnable task) { |
665 |
|
ForkJoinTask<?> job; |
666 |
|
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
676 |
|
* |
677 |
|
* @param task the task to submit |
678 |
|
* @return the task |
679 |
+ |
* @throws NullPointerException if the task is null |
680 |
|
* @throws RejectedExecutionException if the task cannot be |
681 |
|
* scheduled for execution |
626 |
– |
* @throws NullPointerException if the task is null |
682 |
|
*/ |
683 |
|
public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) { |
684 |
|
doSubmit(task); |
686 |
|
} |
687 |
|
|
688 |
|
|
689 |
+ |
/** |
690 |
+ |
* @throws NullPointerException {@inheritDoc} |
691 |
+ |
* @throws RejectedExecutionException {@inheritDoc} |
692 |
+ |
*/ |
693 |
|
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) { |
694 |
|
ArrayList<ForkJoinTask<T>> forkJoinTasks = |
695 |
|
new ArrayList<ForkJoinTask<T>>(tasks.size()); |
796 |
|
final ReentrantLock lock = this.workerLock; |
797 |
|
lock.lock(); |
798 |
|
try { |
799 |
< |
if (!isTerminating()) { |
799 |
> |
if (isProcessingTasks()) { |
800 |
|
int p = this.parallelism; |
801 |
|
this.parallelism = parallelism; |
802 |
|
if (parallelism > p) |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
< |
* Returns the targeted number of worker threads in this pool. |
814 |
> |
* Returns the targeted parallelism level of this pool. |
815 |
|
* |
816 |
< |
* @return the targeted number of worker threads in this pool |
816 |
> |
* @return the targeted parallelism level of this pool |
817 |
|
*/ |
818 |
|
public int getParallelism() { |
819 |
|
return parallelism; |
833 |
|
|
834 |
|
/** |
835 |
|
* Returns the maximum number of threads allowed to exist in the |
836 |
< |
* pool, even if there are insufficient unblocked running threads. |
836 |
> |
* pool. Unless set using {@link #setMaximumPoolSize}, the |
837 |
> |
* maximum is an implementation-defined value designed only to |
838 |
> |
* prevent runaway growth. |
839 |
|
* |
840 |
|
* @return the maximum |
841 |
|
*/ |
845 |
|
|
846 |
|
/** |
847 |
|
* Sets the maximum number of threads allowed to exist in the |
848 |
< |
* pool, even if there are insufficient unblocked running threads. |
849 |
< |
* Setting this value has no effect on current pool size. It |
850 |
< |
* controls construction of new threads. |
848 |
> |
* pool. The given value should normally be greater than or equal |
849 |
> |
* to the {@link #getParallelism parallelism} level. Setting this |
850 |
> |
* value has no effect on current pool size. It controls |
851 |
> |
* construction of new threads. |
852 |
|
* |
853 |
|
* @throws IllegalArgumentException if negative or greater than |
854 |
|
* internal implementation limit |
1053 |
|
* Removes all available unexecuted submitted and forked tasks |
1054 |
|
* from scheduling queues and adds them to the given collection, |
1055 |
|
* without altering their execution status. These may include |
1056 |
< |
* artificially generated or wrapped tasks. This method is designed |
1057 |
< |
* to be invoked only when the pool is known to be |
1056 |
> |
* artificially generated or wrapped tasks. This method is |
1057 |
> |
* designed to be invoked only when the pool is known to be |
1058 |
|
* quiescent. Invocations at other times may not remove all |
1059 |
|
* tasks. A failure encountered while attempting to add elements |
1060 |
|
* to collection {@code c} may result in elements being in |
1106 |
|
} |
1107 |
|
|
1108 |
|
private static String runStateToString(int rs) { |
1109 |
< |
switch(rs) { |
1109 |
> |
switch (rs) { |
1110 |
|
case RUNNING: return "Running"; |
1111 |
|
case SHUTDOWN: return "Shutting down"; |
1112 |
|
case TERMINATING: return "Terminating"; |
1151 |
|
} |
1152 |
|
|
1153 |
|
/** |
1154 |
< |
* Attempts to stop all actively executing tasks, and cancels all |
1155 |
< |
* waiting tasks. Tasks that are in the process of being |
1156 |
< |
* submitted or executed concurrently during the course of this |
1157 |
< |
* method may or may not be rejected. Unlike some other executors, |
1158 |
< |
* this method cancels rather than collects non-executed tasks |
1159 |
< |
* upon termination, so always returns an empty list. However, you |
1160 |
< |
* can use method {@link #drainTasksTo} before invoking this |
1161 |
< |
* method to transfer unexecuted tasks to another collection. |
1154 |
> |
* Attempts to cancel and/or stop all tasks, and reject all |
1155 |
> |
* subsequently submitted tasks. Tasks that are in the process of |
1156 |
> |
* being submitted or executed concurrently during the course of |
1157 |
> |
* this method may or may not be rejected. This method cancels |
1158 |
> |
* both existing and unexecuted tasks, in order to permit |
1159 |
> |
* termination in the presence of task dependencies. So the method |
1160 |
> |
* always returns an empty list (unlike the case for some other |
1161 |
> |
* Executors). |
1162 |
|
* |
1163 |
|
* @return an empty list |
1164 |
|
* @throws SecurityException if a security manager exists and |
1183 |
|
|
1184 |
|
/** |
1185 |
|
* Returns {@code true} if the process of termination has |
1186 |
< |
* commenced but possibly not yet completed. |
1186 |
> |
* commenced but not yet completed. This method may be useful for |
1187 |
> |
* debugging. A return of {@code true} reported a sufficient |
1188 |
> |
* period after shutdown may indicate that submitted tasks have |
1189 |
> |
* ignored or suppressed interruption, causing this executor not |
1190 |
> |
* to properly terminate. |
1191 |
|
* |
1192 |
< |
* @return {@code true} if terminating |
1192 |
> |
* @return {@code true} if terminating but not yet terminated |
1193 |
|
*/ |
1194 |
|
public boolean isTerminating() { |
1195 |
< |
return runStateOf(runControl) >= TERMINATING; |
1195 |
> |
return runStateOf(runControl) == TERMINATING; |
1196 |
|
} |
1197 |
|
|
1198 |
|
/** |
1205 |
|
} |
1206 |
|
|
1207 |
|
/** |
1208 |
+ |
* Returns true if pool is not terminating or terminated. |
1209 |
+ |
* Used internally to suppress execution when terminating. |
1210 |
+ |
*/ |
1211 |
+ |
final boolean isProcessingTasks() { |
1212 |
+ |
return runStateOf(runControl) < TERMINATING; |
1213 |
+ |
} |
1214 |
+ |
|
1215 |
+ |
/** |
1216 |
|
* Blocks until all tasks have completed execution after a shutdown |
1217 |
|
* request, or the timeout occurs, or the current thread is |
1218 |
|
* interrupted, whichever happens first. |
1266 |
|
transitionRunStateTo(TERMINATED); |
1267 |
|
termination.signalAll(); |
1268 |
|
} |
1269 |
< |
else if (!isTerminating()) { |
1269 |
> |
else if (isProcessingTasks()) { |
1270 |
|
tryShrinkWorkerArray(); |
1271 |
|
tryResumeSpare(true); // allow replacement |
1272 |
|
} |
1507 |
|
final void sync(ForkJoinWorkerThread w) { |
1508 |
|
updateStealCount(w); // Transfer w's count while it is idle |
1509 |
|
|
1510 |
< |
while (!w.isShutdown() && !isTerminating() && !suspendIfSpare(w)) { |
1510 |
> |
while (!w.isShutdown() && isProcessingTasks() && !suspendIfSpare(w)) { |
1511 |
|
long prev = w.lastEventCount; |
1512 |
|
WaitQueueNode node = null; |
1513 |
|
WaitQueueNode h; |
1607 |
|
while (spareStack == null || !tryResumeSpare(dec)) { |
1608 |
|
int counts = workerCounts; |
1609 |
|
if (dec || (dec = casWorkerCounts(counts, --counts))) { |
1536 |
– |
// CAS cheat |
1610 |
|
if (!needSpare(counts, maintainParallelism)) |
1611 |
|
break; |
1612 |
|
if (joinMe.status < 0) |
1711 |
|
for (k = 0; k < len && ws[k] != null; ++k) |
1712 |
|
; |
1713 |
|
} |
1714 |
< |
if (k < len && !isTerminating() && (w = createWorker(k)) != null) { |
1714 |
> |
if (k < len && isProcessingTasks() && (w = createWorker(k)) != null) { |
1715 |
|
ws[k] = w; |
1716 |
|
w.start(); |
1717 |
|
} |