20 |
|
import java.util.concurrent.atomic.AtomicLong; |
21 |
|
|
22 |
|
/** |
23 |
< |
* An {@link ExecutorService} for running {@link ForkJoinTask}s. A |
24 |
< |
* ForkJoinPool provides the entry point for submissions from |
23 |
> |
* An {@link ExecutorService} for running {@link ForkJoinTask}s. |
24 |
> |
* A ForkJoinPool provides the entry point for submissions from |
25 |
|
* non-ForkJoinTasks, as well as management and monitoring operations. |
26 |
|
* Normally a single ForkJoinPool is used for a large number of |
27 |
|
* submitted tasks. Otherwise, use would not usually outweigh the |
34 |
|
* (eventually blocking if none exist). This makes them efficient when |
35 |
|
* most tasks spawn other subtasks (as do most ForkJoinTasks), as well |
36 |
|
* as the mixed execution of some plain Runnable- or Callable- based |
37 |
< |
* activities along with ForkJoinTasks. When setting |
38 |
< |
* {@code setAsyncMode}, a ForkJoinPools may also be appropriate for |
39 |
< |
* use with fine-grained tasks that are never joined. Otherwise, other |
40 |
< |
* ExecutorService implementations are typically more appropriate |
41 |
< |
* choices. |
37 |
> |
* activities along with ForkJoinTasks. When setting {@linkplain |
38 |
> |
* #setAsyncMode async mode}, a ForkJoinPool may also be appropriate |
39 |
> |
* for use with fine-grained tasks that are never joined. Otherwise, |
40 |
> |
* other ExecutorService implementations are typically more |
41 |
> |
* appropriate choices. |
42 |
|
* |
43 |
|
* <p>A ForkJoinPool may be constructed with a given parallelism level |
44 |
|
* (target pool size), which it attempts to maintain by dynamically |
45 |
|
* adding, suspending, or resuming threads, even if some tasks are |
46 |
|
* waiting to join others. However, no such adjustments are performed |
47 |
|
* in the face of blocked IO or other unmanaged synchronization. The |
48 |
< |
* nested {@code ManagedBlocker} interface enables extension of |
48 |
> |
* nested {@link ManagedBlocker} interface enables extension of |
49 |
|
* the kinds of synchronization accommodated. The target parallelism |
50 |
< |
* level may also be changed dynamically ({@code setParallelism}) |
50 |
> |
* level may also be changed dynamically ({@link #setParallelism}) |
51 |
|
* and thread construction can be limited using methods |
52 |
< |
* {@code setMaximumPoolSize} and/or |
53 |
< |
* {@code setMaintainsParallelism}. |
52 |
> |
* {@link #setMaximumPoolSize} and/or |
53 |
> |
* {@link #setMaintainsParallelism}. |
54 |
|
* |
55 |
|
* <p>In addition to execution and lifecycle control methods, this |
56 |
|
* class provides status check methods (for example |
57 |
< |
* {@code getStealCount}) that are intended to aid in developing, |
57 |
> |
* {@link #getStealCount}) that are intended to aid in developing, |
58 |
|
* tuning, and monitoring fork/join applications. Also, method |
59 |
< |
* {@code toString} returns indications of pool state in a |
59 |
> |
* {@link #toString} returns indications of pool state in a |
60 |
|
* convenient form for informal monitoring. |
61 |
|
* |
62 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
815 |
|
/** |
816 |
|
* Returns the number of worker threads that have started but not |
817 |
|
* yet terminated. This result returned by this method may differ |
818 |
< |
* from {@code getParallelism} when threads are created to |
818 |
> |
* from {@link #getParallelism} when threads are created to |
819 |
|
* maintain parallelism when others are cooperatively blocked. |
820 |
|
* |
821 |
|
* @return the number of worker threads |
877 |
|
* tasks that are never joined. This mode may be more appropriate |
878 |
|
* than default locally stack-based mode in applications in which |
879 |
|
* worker threads only process asynchronous tasks. This method is |
880 |
< |
* designed to be invoked only when pool is quiescent, and |
880 |
> |
* designed to be invoked only when the pool is quiescent, and |
881 |
|
* typically only before any tasks are submitted. The effects of |
882 |
|
* invocations at other times may be unpredictable. |
883 |
|
* |
884 |
< |
* @param async if true, use locally FIFO scheduling |
884 |
> |
* @param async if {@code true}, use locally FIFO scheduling |
885 |
|
* @return the previous mode |
886 |
+ |
* @see #getAsyncMode |
887 |
|
*/ |
888 |
|
public boolean setAsyncMode(boolean async) { |
889 |
|
boolean oldMode = locallyFifo; |
904 |
|
* scheduling mode for forked tasks that are never joined. |
905 |
|
* |
906 |
|
* @return {@code true} if this pool uses async mode |
907 |
+ |
* @see #setAsyncMode |
908 |
|
*/ |
909 |
|
public boolean getAsyncMode() { |
910 |
|
return locallyFifo; |
1056 |
|
* @param c the collection to transfer elements into |
1057 |
|
* @return the number of elements transferred |
1058 |
|
*/ |
1059 |
< |
protected int drainTasksTo(Collection<ForkJoinTask<?>> c) { |
1059 |
> |
protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
1060 |
|
int n = submissionQueue.drainTo(c); |
1061 |
|
ForkJoinWorkerThread[] ws = workers; |
1062 |
|
if (ws != null) { |
1122 |
|
public void shutdown() { |
1123 |
|
checkPermission(); |
1124 |
|
transitionRunStateTo(SHUTDOWN); |
1125 |
< |
if (canTerminateOnShutdown(runControl)) |
1125 |
> |
if (canTerminateOnShutdown(runControl)) { |
1126 |
> |
if (workers == null) { // shutting down before workers created |
1127 |
> |
final ReentrantLock lock = this.workerLock; |
1128 |
> |
lock.lock(); |
1129 |
> |
try { |
1130 |
> |
if (workers == null) { |
1131 |
> |
terminate(); |
1132 |
> |
transitionRunStateTo(TERMINATED); |
1133 |
> |
termination.signalAll(); |
1134 |
> |
} |
1135 |
> |
|
1136 |
> |
} finally { |
1137 |
> |
lock.unlock(); |
1138 |
> |
} |
1139 |
> |
} |
1140 |
|
terminateOnShutdown(); |
1141 |
+ |
} |
1142 |
|
} |
1143 |
|
|
1144 |
|
/** |
1148 |
|
* method may or may not be rejected. Unlike some other executors, |
1149 |
|
* this method cancels rather than collects non-executed tasks |
1150 |
|
* upon termination, so always returns an empty list. However, you |
1151 |
< |
* can use method {@code drainTasksTo} before invoking this |
1151 |
> |
* can use method {@link #drainTasksTo} before invoking this |
1152 |
|
* method to transfer unexecuted tasks to another collection. |
1153 |
|
* |
1154 |
|
* @return an empty list |