ForkJoinPool

An {@link ExecutorService} for running {@link ForkJoinTask}s. A {@code ForkJoinPool} provides the entry point for submissions from non-{@code ForkJoinTask} clients, as well as management and monitoring operations.

<p>A {@code ForkJoinPool} differs from other kinds of {@link ExecutorService} mainly by virtue of employing <em>work-stealing</em>: 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 {@code ForkJoinTask}s), as well as when many small tasks are submitted to the pool from external clients. Especially when setting <em>asyncMode</em> to true in constructors, {@code ForkJoinPool}s may also be appropriate for use with event-style tasks that are never joined. All worker threads are initialized with {@link Thread#isDaemon} set {@code true}.

<p>A static {@link #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).

<p>For applications that require separate or custom pools, a {@code 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 {@link ManagedBlocker} interface enables extension of the kinds of synchronization accommodated. The default policies may be overridden using a constructor with parameters corresponding to those documented in class {@link ThreadPoolExecutor}.

<p>In addition to execution and lifecycle control methods, this class provides status check methods (for example {@link #getStealCount}) that are intended to aid in developing, tuning, and monitoring fork/join applications. Also, method {@link #toString} returns indications of pool state in a convenient form for informal monitoring.

<p>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 {@code ForkJoinTask}, but overloaded forms also allow mixed execution of plain {@code Runnable}- or {@code 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.

<table class="plain"> <caption>Summary of task execution methods</caption> <tr> <td></td> <th scope="col"> Call from non-fork/join clients</th> <th scope="col"> Call from within fork/join computations</th> </tr> <tr> <th scope="row" style="text-align:left"> Arrange async execution</th> <td> {@link #execute(ForkJoinTask)}</td> <td> {@link ForkJoinTask#fork}</td> </tr> <tr> <th scope="row" style="text-align:left"> Await and obtain result</th> <td> {@link #invoke(ForkJoinTask)}</td> <td> {@link ForkJoinTask#invoke}</td> </tr> <tr> <th scope="row" style="text-align:left"> Arrange exec and obtain Future</th> <td> {@link #submit(ForkJoinTask)}</td> <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td> </tr> </table>

<p>The parameters used to construct the common pool may be controlled by setting the following {@linkplain System#getProperty system properties}: <ul> <li>{@code hunt.concurrency.ForkJoinPool.common.parallelism} - the parallelism level, a non-negative integer <li>{@code hunt.concurrency.ForkJoinPool.common.threadFactory} - the class name of a {@link ForkJoinWorkerThreadFactory}. The {@linkplain ClassLoader#getSystemClassLoader() system class loader} is used to load this class. <li>{@code hunt.concurrency.ForkJoinPool.common.exceptionHandler} - the class name of a {@link UncaughtExceptionHandler}. The {@linkplain ClassLoader#getSystemClassLoader() system class loader} is used to load this class. <li>{@code hunt.concurrency.ForkJoinPool.common.maximumSpares} - the maximum number of allowed extra threads to maintain target parallelism (default 256). </ul> If no thread factory is supplied via a system property, then the common pool uses a factory that uses the system class loader as the {@linkplain Thread#getContextClassLoader() thread context class loader}. In addition, if a {@link SecurityManager} is present, then the common pool uses a factory supplying threads that have no {@link Permissions} enabled.

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 {@code null}. However doing so may cause unjoined tasks to never be executed.

<p><b>Implementation notes</b>: This implementation restricts the maximum number of running threads to 32767. Attempts to create pools with greater than the maximum number result in {@code IllegalArgumentException}.

<p>This implementation rejects submitted tasks (that is, by throwing {@link RejectedExecutionException}) only when the pool is shut down or internal resources have been exhausted.

@author Doug Lea

class ForkJoinPool : AbstractExecutorService {}

Constructors

this
this()

Creates a {@code ForkJoinPool} with parallelism equal to {@link java.lang.Runtime#availableProcessors}, using defaults for all other parameters (see {@link #ForkJoinPool(int, ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, bool, int, int, int, Predicate, long, TimeUnit)}).

this
this(int parallelism)

Creates a {@code ForkJoinPool} with the indicated parallelism level, using defaults for all other parameters (see {@link #ForkJoinPool(int, ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, bool, int, int, int, Predicate, long, TimeUnit)}).

this
this(int parallelism, ForkJoinWorkerThreadFactory factory, UncaughtExceptionHandler handler, bool asyncMode)

Creates a {@code ForkJoinPool} with the given parameters (using defaults for others -- see {@link #ForkJoinPool(int, ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, bool, int, int, int, Predicate, long, TimeUnit)}).

this
this(int parallelism, ForkJoinWorkerThreadFactory factory, UncaughtExceptionHandler handler, bool asyncMode, int corePoolSize, int maximumPoolSize, int minimumRunnable, Predicate!(ForkJoinPool) saturate, Duration keepAliveTime)

Creates a {@code ForkJoinPool} with the given parameters.

Members

Functions

awaitJoin
int awaitJoin(WorkQueue w, IForkJoinTask task, MonoTime deadline)

Helps and/or blocks until the given task is done or timeout. First tries locally helping, then scans other queues for a task produced by one of w's stealers; compensating and blocking if none are found (rescanning if tryCompensate fails).

awaitQuiescence
bool awaitQuiescence(Duration timeout)

If called by a ForkJoinTask operating in this pool, equivalent in effect to {@link ForkJoinTask#helpQuiesce}. Otherwise, waits and/or attempts to assist performing tasks until this pool {@link #isQuiescent} or the indicated timeout elapses.

awaitTermination
bool awaitTermination(Duration timeout)

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. Because the {@link #commonPool()} never terminates until program shutdown, when applied to the common pool, this method is equivalent to {@link #awaitQuiescence(long, TimeUnit)} but always returns {@code false}.

deregisterWorker
void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex)

Final callback from terminating worker, as well as upon failure to construct or start a worker. Removes record of worker from array, and adjusts counts. If pool is shutting down, tries to complete termination.

drainTasksTo
int drainTasksTo(Collection!IForkJoinTask c)

Removes all available unexecuted submitted and forked tasks from scheduling queues and adds them to the given collection, without altering their execution status. These may include artificially generated or wrapped tasks. This method is designed to be invoked only when the pool is known to be quiescent. Invocations at other times may not remove all tasks. A failure encountered while attempting to add elements to collection {@code c} may result in elements being in neither, either or both collections when the associated exception is thrown. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

execute
void execute(IForkJoinTask task)

Arranges for (asynchronous) execution of the given task.

execute
void execute(Runnable task)

@throws NullPointerException if the task is null @throws RejectedExecutionException if the task cannot be scheduled for execution

externalHelpComplete
int externalHelpComplete(ICountedCompleter task, int maxTasks)

Performs helpComplete for an external submitter.

externalPush
void externalPush(IForkJoinTask task)

Adds the given task to a submission queue at submitter's current queue, creating one if null or contended.

getActiveThreadCount
int getActiveThreadCount()

Returns an estimate of the number of threads that are currently stealing or executing tasks. This method may overestimate the number of active threads.

getAsyncMode
bool getAsyncMode()

Returns {@code true} if this pool uses local first-in-first-out scheduling mode for forked tasks that are never joined.

getFactory
ForkJoinWorkerThreadFactory getFactory()

Returns the factory used for constructing new workers.

getParallelism
int getParallelism()

Returns the targeted parallelism level of this pool.

getPoolSize
int getPoolSize()

Returns the number of worker threads that have started but not yet terminated. The result returned by this method may differ from {@link #getParallelism} when threads are created to maintain parallelism when others are cooperatively blocked.

getQueuedSubmissionCount
int getQueuedSubmissionCount()

Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing. This method may take time proportional to the number of submissions.

getQueuedTaskCount
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). This value is only an approximation, obtained by iterating across all threads in the pool. This method may be useful for tuning task granularities.

getRunningThreadCount
int getRunningThreadCount()

Returns an estimate of the number of worker threads that are not blocked waiting to join tasks or for other managed synchronization. This method may overestimate the number of running threads.

getStealCount
long getStealCount()

Returns an estimate of the total number of tasks stolen from one thread's work queue by another. The reported value underestimates the actual total number of steals when the pool is not quiescent. This value may be useful for monitoring and tuning fork/join programs: in general, steal counts should be high enough to keep threads busy, but low enough to avoid overhead and contention across threads.

getUncaughtExceptionHandler
UncaughtExceptionHandler getUncaughtExceptionHandler()

Returns the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks.

hasQueuedSubmissions
bool hasQueuedSubmissions()

Returns {@code true} if there are any tasks submitted to this pool that have not yet begun executing.

helpComplete
int helpComplete(WorkQueue w, ICountedCompleter task, int maxTasks)

Tries to steal and run tasks within the target's computation. The maxTasks argument supports external usages; internal calls use zero, allowing unbounded steps (external calls trap non-positive values).

helpQuiescePool
void helpQuiescePool(WorkQueue w)

Runs tasks until {@code isQuiescent()}. Rather than blocking when tasks cannot be found, rescans until all others cannot find tasks either.

invoke
T invoke(ForkJoinTask!(T) task)

Performs the given task, returning its result upon completion. If the computation encounters an unchecked Exception or Error, it is rethrown as the outcome of this invocation. Rethrown exceptions behave in the same way as regular exceptions, but, when possible, contain stack traces (as displayed for example using {@code ex.printStackTrace()}) of both the current thread as well as the thread actually encountering the exception; minimally only the latter.

invokeAll
List!(Future!(T)) invokeAll(Collection!(Callable!(T)) tasks)

@throws NullPointerException {@inheritDoc} @throws RejectedExecutionException {@inheritDoc}

isQuiescent
bool isQuiescent()

Returns {@code true} if all worker threads are currently idle. An idle worker is one that cannot obtain a task to execute because none are available to steal from other threads, and there are no pending submissions to the pool. This method is conservative; it might not return {@code true} immediately upon idleness of all threads, but will eventually become true if threads remain inactive.

isShutdown
bool isShutdown()

Returns {@code true} if this pool has been shut down.

isTerminated
bool isTerminated()

Returns {@code true} if all tasks have completed following shut down.

isTerminating
bool isTerminating()

Returns {@code true} if the process of termination has commenced but not yet completed. This method may be useful for debugging. A return of {@code true} reported a sufficient period after shutdown may indicate that submitted tasks have ignored or suppressed interruption, or are waiting for I/O, causing this executor not to properly terminate. (See the advisory notes for class {@link ForkJoinTask} stating that tasks should not normally entail blocking operations. But if they do, they must abort them on interrupt.)

newTaskFor
RunnableFuture!(T) newTaskFor(Runnable runnable, T value)
Undocumented in source. Be warned that the author may not have intended to support it.
newTaskFor
RunnableFuture!(T) newTaskFor(Callable!(T) callable)
Undocumented in source. Be warned that the author may not have intended to support it.
nextTaskFor
IForkJoinTask nextTaskFor(WorkQueue w)

Gets and removes a local or stolen task for the given worker.

pollSubmission
IForkJoinTask pollSubmission()

Removes and returns the next unexecuted submission if one is available. This method may be useful in extensions to this class that re-assign work in systems with multiple pools.

registerWorker
WorkQueue registerWorker(ForkJoinWorkerThread wt)

Callback from ForkJoinWorkerThread constructor to establish and record its WorkQueue.

runWorker
void runWorker(WorkQueue w)

Top-level runloop for workers, called by ForkJoinWorkerThread.run. See above for explanation.

shutdown
void shutdown()

Possibly initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no effect on execution state if this is the {@link #commonPool()}, and no additional effect if already shut down. Tasks that are in the process of being submitted concurrently during the course of this method may or may not be rejected.

shutdownNow
List!(Runnable) shutdownNow()

Possibly attempts to cancel and/or stop all tasks, and reject all subsequently submitted tasks. Invocation has no effect on execution state if this is the {@link #commonPool()}, and no additional effect if already shut down. Otherwise, tasks that are in the process of being submitted or executed concurrently during the course of this method may or may not be rejected. This method cancels both existing and unexecuted tasks, in order to permit termination in the presence of task dependencies. So the method always returns an empty list (unlike the case for some other Executors).

signalWork
void signalWork()

Tries to create or release a worker if too few are running.

submitTask
ForkJoinTask!(T) submitTask(ForkJoinTask!(T) task)

Submits a ForkJoinTask for execution.

submitTask
ForkJoinTask!(T) submitTask(Callable!(T) task)

@throws NullPointerException if the task is null @throws RejectedExecutionException if the task cannot be scheduled for execution

submitTask
ForkJoinTask!(T) submitTask(Runnable task, T result)

@throws NullPointerException if the task is null @throws RejectedExecutionException if the task cannot be scheduled for execution

submitTask
IForkJoinTask submitTask(Runnable task)

@throws NullPointerException if the task is null @throws RejectedExecutionException if the task cannot be scheduled for execution

toString
string toString()

Returns a string identifying this pool, as well as its state, including indications of run state, parallelism level, and worker and task counts.

tryExternalUnpush
bool tryExternalUnpush(IForkJoinTask task)

Performs tryUnpush for an external submitter.

Static functions

commonPool
ForkJoinPool commonPool()

Returns the common pool instance. This pool is statically constructed; its run state is unaffected by attempts to {@link #shutdown} or {@link #shutdownNow}. However this pool and any ongoing processing are automatically terminated upon program {@link System#exit}. Any program that relies on asynchronous task processing to complete before program termination should invoke {@code commonPool().}{@link #awaitQuiescence awaitQuiescence}, before exit.

commonSubmitterQueue
WorkQueue commonSubmitterQueue()

Returns common pool queue for an external thread.

getCommonPoolParallelism
int getCommonPoolParallelism()

Returns the targeted parallelism level of the common pool.

getSurplusQueuedTaskCount
int getSurplusQueuedTaskCount()

Returns a cheap heuristic guide for task partitioning when programmers, frameworks, tools, or languages have little or no idea about task granularity. In essence, by offering this method, we ask users only about tradeoffs in overhead vs expected throughput and its variance, rather than how finely to partition tasks.

helpAsyncBlocker
void helpAsyncBlocker(Executor e, ManagedBlocker blocker)

If the given executor is a ForkJoinPool, poll and execute AsynchronousCompletionTasks from worker's queue until none are available or blocker is released.

managedBlock
void managedBlock(ManagedBlocker blocker)

Runs the given possibly blocking task. When {@linkplain ForkJoinTask#inForkJoinPool() running in a ForkJoinPool}, this method possibly arranges for a spare thread to be activated if necessary to ensure sufficient parallelism while the current thread is blocked in {@link ManagedBlocker#block blocker.block()}.

quiesceCommonPool
void quiesceCommonPool()

Waits and/or attempts to assist performing tasks indefinitely until the {@link #commonPool()} {@link #isQuiescent}.

Static variables

COMMON_PARALLELISM
int COMMON_PARALLELISM;

Common pool parallelism. To allow simpler use and management when common pool threads are disabled, we allow the underlying common.parallelism field to be zero, but in that case still report parallelism as 1 to reflect resulting caller-runs mechanics.

common
ForkJoinPool common;

Common (static) pool. Non-null for use unless a static construction exception, but internal usages null-check on use to paranoically avoid potential initialization circularities as well as to simplify generated code.

defaultForkJoinWorkerThreadFactory
ForkJoinWorkerThreadFactory defaultForkJoinWorkerThreadFactory;

Creates a new ForkJoinWorkerThread. This factory is used unless overridden in ForkJoinPool constructors.

Variables

bounds
int bounds;
Undocumented in source.
ctl
long ctl;
Undocumented in source.
factory
ForkJoinWorkerThreadFactory factory;
Undocumented in source.
indexSeed
int indexSeed;
Undocumented in source.
keepAlive
Duration keepAlive;
Undocumented in source.
mode
int mode;
Undocumented in source.
saturate
Predicate!(ForkJoinPool) saturate;
Undocumented in source.
stealCount
long stealCount;
Undocumented in source.
ueh
UncaughtExceptionHandler ueh;
Undocumented in source.
workQueues
WorkQueue[] workQueues;
Undocumented in source.
workerNameLocker
Object workerNameLocker;
Undocumented in source.
workerNamePrefix
string workerNamePrefix;
Undocumented in source.

Inherited Members

From AbstractExecutorService

newTaskFor
RunnableFuture!(T) newTaskFor(Runnable runnable, T value)

Returns a {@code RunnableFuture} for the given runnable and default value.

newTaskFor
RunnableFuture!(T) newTaskFor(Runnable runnable)
Undocumented in source. Be warned that the author may not have intended to support it.
newTaskFor
RunnableFuture!(T) newTaskFor(Callable!(T) callable)

Returns a {@code RunnableFuture} for the given callable task.

submit
Future!(void) submit(Runnable task)

@throws RejectedExecutionException {@inheritDoc} @throws NullPointerException {@inheritDoc}

submit
Future!(T) submit(Runnable task, T result)

@throws RejectedExecutionException {@inheritDoc} @throws NullPointerException {@inheritDoc}

submit
Future!(T) submit(Callable!(T) task)

@throws RejectedExecutionException {@inheritDoc} @throws NullPointerException {@inheritDoc}

invokeAny
T invokeAny(Collection!(Callable!(T)) tasks)
Undocumented in source. Be warned that the author may not have intended to support it.
invokeAny
T invokeAny(Collection!(Callable!(T)) tasks, Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.
invokeAll
List!(Future!(T)) invokeAll(Collection!(Callable!(T)) tasks)
Undocumented in source. Be warned that the author may not have intended to support it.
invokeAll
List!(Future!(T)) invokeAll(Collection!(Callable!(T)) tasks, Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.

Meta