An
ExecutorService
for running
ForkJoinTask
s. A
ForkJoinPool
provides the entry point for submissions from non-
ForkJoinTask
clients, as well as management and monitoring operations.
A ForkJoinPool
differs from other kinds of ExecutorService
mainly by virtue of employing work-stealing: all threads in the pool attempt to find and execute subtasks created by other active tasks (eventually blocking waiting for work if none exist). This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTask
s). When setting asyncMode to true in constructors, ForkJoinPool
s may also be appropriate for use with event-style tasks that are never joined.
A ForkJoinPool
is constructed with a given target parallelism level; by default, equal to the number of available processors. The pool attempts to maintain enough active (or available) threads by dynamically adding, suspending, or resuming internal worker threads, even if some tasks are stalled waiting to join others. However, no such adjustments are guaranteed in the face of blocked IO or other unmanaged synchronization. The nested ForkJoinPool.ManagedBlocker
interface enables extension of the kinds of synchronization accommodated.
In addition to execution and lifecycle control methods, this class provides status check methods (for example getStealCount()
) that are intended to aid in developing, tuning, and monitoring fork/join applications. Also, method toString()
returns indications of pool state in a convenient form for informal monitoring.
As is the case with other ExecutorServices, there are three main task execution methods summarized in the following table. These are designed to be used by clients not already engaged in fork/join computations in the current pool. The main forms of these methods accept instances of ForkJoinTask
, but overloaded forms also allow mixed execution of plain Runnable
- or Callable
- based activities as well. However, tasks that are already executing in a pool should normally NOT use these pool execution methods, but instead use the within-computation forms listed in the table.
Sample Usage. Normally a single ForkJoinPool
is used for all parallel task execution in a program or subsystem. Otherwise, use would not usually outweigh the construction and bookkeeping overhead of creating a large set of threads. For example, a common pool could be used for the SortTasks
illustrated in RecursiveAction
. Because ForkJoinPool
uses threads in daemon mode, there is typically no need to explicitly shutdown
such a pool upon program exit.
static final ForkJoinPool mainPool = new ForkJoinPool(); ... public void sort(long[] array) { mainPool.invoke(new SortTask(array, 0, array.length)); }
Implementation notes: This implementation restricts the maximum number of running threads to 32767. Attempts to create pools with greater than the maximum number result in IllegalArgumentException
.
This implementation rejects submitted tasks (that is, by throwing RejectedExecutionException
) only when the pool is shut down or internal resources have been exhausted.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4