19 |
|
|
20 |
|
/** |
21 |
|
* An {@link ExecutorService} for running {@link ForkJoinTask}s. |
22 |
< |
* A ForkJoinPool provides the entry point for submissions from |
23 |
< |
* non-ForkJoinTasks, as well as management and monitoring operations. |
24 |
< |
* Normally a single ForkJoinPool is used for a large number of |
25 |
< |
* submitted tasks. Otherwise, use would not usually outweigh the |
26 |
< |
* construction and bookkeeping overhead of creating a large set of |
27 |
< |
* threads. |
22 |
> |
* A {@code ForkJoinPool} provides the entry point for submissions |
23 |
> |
* from non-{@code ForkJoinTask}s, as well as management and |
24 |
> |
* monitoring operations. Normally a single {@code ForkJoinPool} is |
25 |
> |
* used for a large number of submitted tasks. Otherwise, use would |
26 |
> |
* not usually outweigh the construction and bookkeeping overhead of |
27 |
> |
* creating a large set of threads. |
28 |
|
* |
29 |
< |
* <p>ForkJoinPools differ from other kinds of Executors mainly in |
30 |
< |
* that they provide <em>work-stealing</em>: all threads in the pool |
31 |
< |
* attempt to find and execute subtasks created by other active tasks |
32 |
< |
* (eventually blocking if none exist). This makes them efficient when |
33 |
< |
* most tasks spawn other subtasks (as do most ForkJoinTasks), as well |
34 |
< |
* as the mixed execution of some plain Runnable- or Callable- based |
35 |
< |
* activities along with ForkJoinTasks. When setting {@linkplain |
36 |
< |
* #setAsyncMode async mode}, a ForkJoinPool may also be appropriate |
37 |
< |
* for use with fine-grained tasks that are never joined. Otherwise, |
38 |
< |
* other ExecutorService implementations are typically more |
29 |
> |
* <p>{@code ForkJoinPool}s differ from other kinds of {@link |
30 |
> |
* Executor}s mainly in that they provide <em>work-stealing</em>: all |
31 |
> |
* threads in the pool attempt to find and execute subtasks created by |
32 |
> |
* other active tasks (eventually blocking if none exist). This makes |
33 |
> |
* them efficient when most tasks spawn other subtasks (as do most |
34 |
> |
* {@code ForkJoinTask}s), as well as the mixed execution of some |
35 |
> |
* plain {@code Runnable}- or {@code Callable}- based activities along |
36 |
> |
* with {@code ForkJoinTask}s. When setting {@linkplain #setAsyncMode |
37 |
> |
* async mode}, a {@code ForkJoinPool} may also be appropriate for use |
38 |
> |
* with fine-grained tasks that are never joined. Otherwise, other |
39 |
> |
* {@code ExecutorService} implementations are typically more |
40 |
|
* appropriate choices. |
41 |
|
* |
42 |
< |
* <p>A ForkJoinPool may be constructed with a given parallelism level |
43 |
< |
* (target pool size), which it attempts to maintain by dynamically |
44 |
< |
* adding, suspending, or resuming threads, even if some tasks are |
45 |
< |
* waiting to join others. However, no such adjustments are performed |
46 |
< |
* in the face of blocked IO or other unmanaged synchronization. The |
47 |
< |
* nested {@link ManagedBlocker} interface enables extension of |
48 |
< |
* the kinds of synchronization accommodated. The target parallelism |
49 |
< |
* level may also be changed dynamically ({@link #setParallelism}) |
50 |
< |
* and thread construction can be limited using methods |
51 |
< |
* {@link #setMaximumPoolSize} and/or |
52 |
< |
* {@link #setMaintainsParallelism}. |
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 |
46 |
> |
* are performed in the face of blocked IO or other unmanaged |
47 |
> |
* synchronization. The nested {@link ManagedBlocker} interface |
48 |
> |
* enables extension of the kinds of synchronization accommodated. |
49 |
> |
* The target parallelism level may also be changed dynamically |
50 |
> |
* ({@link #setParallelism}) and thread construction can be limited |
51 |
> |
* using methods {@link #setMaximumPoolSize} and/or {@link |
52 |
> |
* #setMaintainsParallelism}. |
53 |
|
* |
54 |
|
* <p>In addition to execution and lifecycle control methods, this |
55 |
|
* class provides status check methods (for example |
61 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
62 |
|
* maximum number of running threads to 32767. Attempts to create |
63 |
|
* pools with greater than the maximum result in |
64 |
< |
* IllegalArgumentExceptions. |
64 |
> |
* {@code IllegalArgumentException}. |
65 |
|
* |
66 |
|
* @since 1.7 |
67 |
|
* @author Doug Lea |
80 |
|
private static final int MAX_THREADS = 0x7FFF; |
81 |
|
|
82 |
|
/** |
83 |
< |
* Factory for creating new ForkJoinWorkerThreads. A |
84 |
< |
* ForkJoinWorkerThreadFactory must be defined and used for |
85 |
< |
* ForkJoinWorkerThread subclasses that extend base functionality |
86 |
< |
* or initialize threads with different contexts. |
83 |
> |
* Factory for creating new {@link ForkJoinWorkerThread}s. |
84 |
> |
* A {@code ForkJoinWorkerThreadFactory} must be defined and used |
85 |
> |
* for {@code ForkJoinWorkerThread} subclasses that extend base |
86 |
> |
* functionality or initialize threads with different contexts. |
87 |
|
*/ |
88 |
|
public static interface ForkJoinWorkerThreadFactory { |
89 |
|
/** |
341 |
|
// Constructors |
342 |
|
|
343 |
|
/** |
344 |
< |
* Creates a ForkJoinPool with a pool size equal to the number of |
345 |
< |
* processors available on the system, using the default |
346 |
< |
* ForkJoinWorkerThreadFactory. |
344 |
> |
* Creates a {@code ForkJoinPool} with a pool size equal to the |
345 |
> |
* number of processors available on the system, using the |
346 |
> |
* {@linkplain #defaultForkJoinWorkerThreadFactory default thread factory}. |
347 |
|
* |
348 |
|
* @throws SecurityException if a security manager exists and |
349 |
|
* the caller is not permitted to modify threads |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
< |
* Creates a ForkJoinPool with the indicated parallelism level |
360 |
< |
* threads and using the default ForkJoinWorkerThreadFactory. |
359 |
> |
* Creates a {@code ForkJoinPool} with the indicated parallelism level |
360 |
> |
* threads and using the |
361 |
> |
* {@linkplain #defaultForkJoinWorkerThreadFactory default thread factory}. |
362 |
|
* |
363 |
|
* @param parallelism the number of worker threads |
364 |
|
* @throws IllegalArgumentException if parallelism less than or |
373 |
|
} |
374 |
|
|
375 |
|
/** |
376 |
< |
* Creates a ForkJoinPool with parallelism equal to the number of |
377 |
< |
* processors available on the system and using the given |
378 |
< |
* ForkJoinWorkerThreadFactory. |
376 |
> |
* Creates a {@code ForkJoinPool} with parallelism equal to the |
377 |
> |
* number of processors available on the system and using the |
378 |
> |
* given thread factory. |
379 |
|
* |
380 |
|
* @param factory the factory for creating new threads |
381 |
|
* @throws NullPointerException if factory is null |
389 |
|
} |
390 |
|
|
391 |
|
/** |
392 |
< |
* Creates a ForkJoinPool with the given parallelism and factory. |
392 |
> |
* Creates a {@code ForkJoinPool} with the given parallelism and |
393 |
> |
* thread factory. |
394 |
|
* |
395 |
|
* @param parallelism the targeted number of worker threads |
396 |
|
* @param factory the factory for creating new threads |
424 |
|
* Creates a new worker thread using factory. |
425 |
|
* |
426 |
|
* @param index the index to assign worker |
427 |
< |
* @return new worker, or null of factory failed |
427 |
> |
* @return new worker, or null if factory failed |
428 |
|
*/ |
429 |
|
private ForkJoinWorkerThread createWorker(int index) { |
430 |
|
Thread.UncaughtExceptionHandler h = ueh; |
579 |
|
* @throws NullPointerException if task is null |
580 |
|
* @throws RejectedExecutionException if pool is shut down |
581 |
|
*/ |
582 |
< |
public <T> void execute(ForkJoinTask<T> task) { |
582 |
> |
public void execute(ForkJoinTask<?> task) { |
583 |
|
doSubmit(task); |
584 |
|
} |
585 |
|
|
788 |
|
* Setting this value has no effect on current pool size. It |
789 |
|
* controls construction of new threads. |
790 |
|
* |
791 |
< |
* @throws IllegalArgumentException if negative or greater then |
791 |
> |
* @throws IllegalArgumentException if negative or greater than |
792 |
|
* internal implementation limit |
793 |
|
*/ |
794 |
|
public void setMaximumPoolSize(int newMax) { |
956 |
|
} |
957 |
|
|
958 |
|
/** |
959 |
< |
* Returns an estimate of the number tasks submitted to this pool |
960 |
< |
* that have not yet begun executing. This method takes time |
959 |
> |
* Returns an estimate of the number of tasks submitted to this |
960 |
> |
* pool that have not yet begun executing. This method takes time |
961 |
|
* proportional to the number of submissions. |
962 |
|
* |
963 |
|
* @return the number of queued submissions |
991 |
|
* Removes all available unexecuted submitted and forked tasks |
992 |
|
* from scheduling queues and adds them to the given collection, |
993 |
|
* without altering their execution status. These may include |
994 |
< |
* artificially generated or wrapped tasks. This method is designed |
995 |
< |
* to be invoked only when the pool is known to be |
994 |
> |
* artificially generated or wrapped tasks. This method is |
995 |
> |
* designed to be invoked only when the pool is known to be |
996 |
|
* quiescent. Invocations at other times may not remove all |
997 |
|
* tasks. A failure encountered while attempting to add elements |
998 |
|
* to collection {@code c} may result in elements being in |
1736 |
|
|
1737 |
|
/** |
1738 |
|
* Interface for extending managed parallelism for tasks running |
1739 |
< |
* in ForkJoinPools. A ManagedBlocker provides two methods. |
1739 |
> |
* in {@link ForkJoinPool}s. |
1740 |
> |
* |
1741 |
> |
* <p>A {@code ManagedBlocker} provides two methods. |
1742 |
|
* Method {@code isReleasable} must return {@code true} if |
1743 |
|
* blocking is not necessary. Method {@code block} blocks the |
1744 |
|
* current thread if necessary (perhaps internally invoking |
1745 |
< |
* {@code isReleasable} before actually blocking.). |
1745 |
> |
* {@code isReleasable} before actually blocking). |
1746 |
|
* |
1747 |
|
* <p>For example, here is a ManagedBlocker based on a |
1748 |
|
* ReentrantLock: |
1781 |
|
|
1782 |
|
/** |
1783 |
|
* Blocks in accord with the given blocker. If the current thread |
1784 |
< |
* is a ForkJoinWorkerThread, this method possibly arranges for a |
1785 |
< |
* spare thread to be activated if necessary to ensure parallelism |
1786 |
< |
* while the current thread is blocked. If |
1787 |
< |
* {@code maintainParallelism} is {@code true} and the pool supports |
1788 |
< |
* it ({@link #getMaintainsParallelism}), this method attempts to |
1789 |
< |
* maintain the pool's nominal parallelism. Otherwise it activates |
1790 |
< |
* a thread only if necessary to avoid complete starvation. This |
1791 |
< |
* option may be preferable when blockages use timeouts, or are |
1792 |
< |
* almost always brief. |
1784 |
> |
* is a {@link ForkJoinWorkerThread}, this method possibly |
1785 |
> |
* arranges for a spare thread to be activated if necessary to |
1786 |
> |
* ensure parallelism while the current thread is blocked. |
1787 |
> |
* |
1788 |
> |
* <p>If {@code maintainParallelism} is {@code true} and the pool |
1789 |
> |
* supports it ({@link #getMaintainsParallelism}), this method |
1790 |
> |
* attempts to maintain the pool's nominal parallelism. Otherwise |
1791 |
> |
* it activates a thread only if necessary to avoid complete |
1792 |
> |
* starvation. This option may be preferable when blockages use |
1793 |
> |
* timeouts, or are almost always brief. |
1794 |
|
* |
1795 |
< |
* <p> If the caller is not a ForkJoinTask, this method is behaviorally |
1796 |
< |
* equivalent to |
1795 |
> |
* <p>If the caller is not a {@link ForkJoinTask}, this method is |
1796 |
> |
* behaviorally equivalent to |
1797 |
|
* <pre> {@code |
1798 |
|
* while (!blocker.isReleasable()) |
1799 |
|
* if (blocker.block()) |
1800 |
|
* return; |
1801 |
|
* }</pre> |
1802 |
< |
* If the caller is a ForkJoinTask, then the pool may first |
1803 |
< |
* be expanded to ensure parallelism, and later adjusted. |
1802 |
> |
* |
1803 |
> |
* If the caller is a {@code ForkJoinTask}, then the pool may |
1804 |
> |
* first be expanded to ensure parallelism, and later adjusted. |
1805 |
|
* |
1806 |
|
* @param blocker the blocker |
1807 |
|
* @param maintainParallelism if {@code true} and supported by |