ForkJoinPool

public class ForkJoinPool extends AbstractExecutorService

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 tasks submitted to the pool and/or 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 ForkJoinTasks), as well as when many small tasks are submitted to the pool from external clients. Especially when setting asyncMode to true in constructors, ForkJoinPools may also be appropriate for use with event-style tasks that are never joined.

A static commonPool() is available and appropriate for most applications. The common pool is used by any ForkJoinTask that is not explicitly submitted to a specified pool. Using the common pool normally reduces resource usage (its threads are slowly reclaimed during periods of non-use, and reinstated upon subsequent use).

For applications that require separate or custom pools, a ForkJoinPool may be 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 I/O 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 primarily 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 instead use the within-computation forms listed in the table unless using async event-style tasks that are not usually joined, in which case there is little difference among choice of methods.

Summary of task execution methods
Call from non-fork/join clients Call from within fork/join computations
Arrange async execution execute(ForkJoinTask) fork()
Await and obtain result invoke(ForkJoinTask) invoke()
Arrange exec and obtain Future submit(ForkJoinTask) fork() (ForkJoinTasks are Futures)

The common pool is by default constructed with default parameters, but these may be controlled by setting three {@linkplain System#getProperty system properties}:

  • java.util.concurrent.ForkJoinPool.common.parallelism - the parallelism level, a non-negative integer
  • java.util.concurrent.ForkJoinPool.common.threadFactory - the class name of a ForkJoinPool.ForkJoinWorkerThreadFactory
  • java.util.concurrent.ForkJoinPool.common.exceptionHandler - the class name of a Thread.UncaughtExceptionHandler
  • java.util.concurrent.ForkJoinPool.common.maximumSpares - the maximum number of allowed extra threads to maintain target parallelism (default 256).
If a SecurityManager is present and no factory is specified, then the default pool uses a factory supplying threads that have no ERROR(/Permissions) enabled. The system class loader is used to load these classes. Upon any error in establishing these settings, default parameters are used. It is possible to disable or limit the use of threads in the common pool by setting the parallelism property to zero, and/or using a factory that may return null. However doing so may cause unjoined tasks to never be executed.

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.

Nested Class Summary

interface ForkJoinPool.ForkJoinWorkerThreadFactory Factory for creating new ForkJoinWorkerThreads. 
interface ForkJoinPool.ManagedBlocker Interface for extending managed parallelism for tasks running in ForkJoinPools. 

Field Summary

public static final ForkJoinPool.ForkJoinWorkerThreadFactory defaultForkJoinWorkerThreadFactory Creates a new ForkJoinWorkerThread.

Public Constructor Summary

ForkJoinPool()
Creates a ForkJoinPool with parallelism equal to availableProcessors(), using the {@linkplain #defaultForkJoinWorkerThreadFactory default thread factory}, no UncaughtExceptionHandler, and non-async LIFO processing mode.
ForkJoinPool(int parallelism)
Creates a ForkJoinPool with the indicated parallelism level, the {@linkplain #defaultForkJoinWorkerThreadFactory default thread factory}, no UncaughtExceptionHandler, and non-async LIFO processing mode.
ForkJoinPool(int parallelism, ForkJoinPool.ForkJoinWorkerThreadFactory factory, Thread.UncaughtExceptionHandler handler, boolean asyncMode)
Creates a ForkJoinPool with the given parameters.

Public Method Summary

boolean
awaitQuiescence(long timeout, TimeUnit unit)
If called by a ForkJoinTask operating in this pool, equivalent in effect to helpQuiesce().
boolean
awaitTermination(long timeout, TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
static ForkJoinPool
commonPool()
Returns the common pool instance.
void
void
execute(ForkJoinTask<?> task)
Arranges for (asynchronous) execution of the given task.
int
getActiveThreadCount()
Returns an estimate of the number of threads that are currently stealing or executing tasks.
boolean
getAsyncMode()
Returns true if this pool uses local first-in-first-out scheduling mode for forked tasks that are never joined.
static int
getCommonPoolParallelism()
Returns the targeted parallelism level of the common pool.
ForkJoinPool.ForkJoinWorkerThreadFactory
getFactory()
Returns the factory used for constructing new workers.
int
getParallelism()
Returns the targeted parallelism level of this pool.
int
getPoolSize()
Returns the number of worker threads that have started but not yet terminated.
int
getQueuedSubmissionCount()
Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing.
long
getQueuedTaskCount()
Returns an estimate of the total number of tasks currently held in queues by worker threads (but not including tasks submitted to the pool that have not begun executing).
int
getRunningThreadCount()
Returns an estimate of the number of worker threads that are not blocked waiting to join tasks or for other managed synchronization.
long
getStealCount()
Returns an estimate of the total number of tasks stolen from one thread's work queue by another.
Thread.UncaughtExceptionHandler
getUncaughtExceptionHandler()
Returns the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks.
boolean
hasQueuedSubmissions()
Returns true if there are any tasks submitted to this pool that have not yet begun executing.
<T> T
invoke(ForkJoinTask<T> task)
Performs the given task, returning its result upon completion.
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)
Executes the given tasks, returning a list of Futures holding their status and results when all complete.
boolean
isQuiescent()
Returns true if all worker threads are currently idle.
boolean
isShutdown()
Returns true if this pool has been shut down.
boolean
isTerminated()
Returns true if all tasks have completed following shut down.
boolean