ForkJoinPool (Java Platform SE 7 ) (original) (raw)
An ExecutorService for running ForkJoinTasks. 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 nestedForkJoinPool.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 examplegetStealCount()) that are intended to aid in developing, tuning, and monitoring fork/join applications. Also, methodtoString() 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.
Call from non-fork/join clients | Call from within fork/join computations | |
---|---|---|
Arrange async execution | execute(ForkJoinTask) | ForkJoinTask.fork() |
Await and obtain result | invoke(ForkJoinTask) | ForkJoinTask.invoke() |
Arrange exec and obtain Future | submit(ForkJoinTask) | ForkJoinTask.fork() (ForkJoinTasks are Futures) |
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 inIllegalArgumentException
.
This implementation rejects submitted tasks (that is, by throwingRejectedExecutionException) only when the pool is shut down or internal resources have been exhausted.