GeezerVison is showing docs.oracle.com — ForkJoinPool (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util.concurrent
Class ForkJoinPool
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
boolean |
awaitQuiescence(long timeout,
TimeUnit unit) |
|
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.
|
protected int |
drainTasksTo(Collection<? super ForkJoinTask<?>> c) |
Removes all available unexecuted submitted and forked tasks
from scheduling queues and adds them to the given collection,
without altering their execution status.
|
void |
execute(ForkJoinTask<?> task) |
Arranges for (asynchronous) execution of the given task.
|
void |
execute(Runnable task) |
Executes the given command at some time in the future.
|
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 |
isTerminating() |
Returns true if the process of termination has
commenced but not yet completed.
|
static void |
managedBlock(ForkJoinPool.ManagedBlocker blocker) |
Runs the given possibly blocking task.
|
protected <T> RunnableFuture<T> |
newTaskFor(Callable<T> callable) |
Returns a RunnableFuture for the given callable task.
|
protected <T> RunnableFuture<T> |
newTaskFor(Runnable runnable,
T value) |
Returns a RunnableFuture for the given runnable and default
value.
|
protected ForkJoinTask<?> |
pollSubmission() |
Removes and returns the next unexecuted submission if one is
available.
|
void |
shutdown() |
Possibly initiates an orderly shutdown in which previously
submitted tasks are executed, but no new tasks will be
accepted.
|
List<Runnable> |
shutdownNow() |
Possibly attempts to cancel and/or stop all tasks, and reject
all subsequently submitted tasks.
|
<T> ForkJoinTask<T> |
submit(Callable<T> task) |
Submits a value-returning task for execution and returns a
Future representing the pending results of the task.
|
<T> ForkJoinTask<T> |
submit(ForkJoinTask<T> task) |
Submits a ForkJoinTask for execution.
|
ForkJoinTask<?> |
submit(Runnable task) |
Submits a Runnable task for execution and returns a Future
representing that task.
|
<T> ForkJoinTask<T> |
submit(Runnable task,
T result) |
Submits a Runnable task for execution and returns a Future
representing that task.
|
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.
|
-
-
Constructor Detail
-
ForkJoinPool
public ForkJoinPool()
- Throws:
SecurityException - if a security manager exists and
the caller is not permitted to modify threads
because it does not hold RuntimePermission("modifyThread")
-
ForkJoinPool
public ForkJoinPool(int parallelism,
ForkJoinPool.ForkJoinWorkerThreadFactory factory,
Thread.UncaughtExceptionHandler handler,
boolean asyncMode)
Creates a ForkJoinPool with the given parameters.
- Parameters:
parallelism - the parallelism level. For default value,
use Runtime.availableProcessors().
factory - the factory for creating new threads. For default value,
use defaultForkJoinWorkerThreadFactory.
handler - the handler for internal worker threads that
terminate due to unrecoverable errors encountered while executing
tasks. For default value, use null.
asyncMode - if true,
establishes local first-in-first-out scheduling mode for forked
tasks that are never joined. This mode may be more appropriate
than default locally stack-based mode in applications in which
worker threads only process event-style asynchronous tasks.
For default value, use false.
- Throws:
IllegalArgumentException - if parallelism less than or
equal to zero, or greater than implementation limit
NullPointerException - if the factory is null
SecurityException - if a security manager exists and
the caller is not permitted to modify threads
because it does not hold RuntimePermission("modifyThread")
-
Method Detail
-
invoke
public <T> 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 ex.printStackTrace()) of both the current thread
as well as the thread actually encountering the exception;
minimally only the latter.
- Type Parameters:
T - the type of the task's result
- Parameters:
task - the task
- Returns:
- the task's result
- Throws:
NullPointerException - if the task is null
RejectedExecutionException - if the task cannot be
scheduled for execution
-
execute
public void execute(Runnable task)
Description copied from interface: Executor
Executes the given command at some time in the future. The command
may execute in a new thread, in a pooled thread, or in the calling
thread, at the discretion of the Executor implementation.
- Parameters:
task - the runnable task
- Throws:
NullPointerException - if the task is null
RejectedExecutionException - if the task cannot be
scheduled for execution
-
submit
public <T> ForkJoinTask<T> submit(Runnable task,
T result)
Submits a Runnable task for execution and returns a Future
representing that task. The Future's get method will
return the given result upon successful completion.
- Specified by:
submit in interface ExecutorService
- Overrides:
submit in class AbstractExecutorService
- Type Parameters:
T - the type of the result
- Parameters:
task - the task to submit
result - the result to return
- Returns:
- a Future representing pending completion of the task
- Throws:
NullPointerException - if the task is null
RejectedExecutionException - if the task cannot be
scheduled for execution
-
getPoolSize
public int getPoolSize()
Returns the number of worker threads that have started but not
yet terminated. The result returned by this method may differ
from getParallelism() when threads are created to
maintain parallelism when others are cooperatively blocked.
- Returns:
- the number of worker threads
-
getRunningThreadCount
public 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.
- Returns:
- the number of worker threads
-
isQuiescent
public boolean isQuiescent()
Returns 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 true immediately upon
idleness of all threads, but will eventually become true if
threads remain inactive.
- Returns:
true if all threads are currently idle
-
getStealCount
public 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.
- Returns:
- the number of steals
-
getQueuedTaskCount
public 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.
- Returns:
- the number of queued tasks
-
getQueuedSubmissionCount
public 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.
- Returns:
- the number of queued submissions
-
pollSubmission
protected ForkJoinTask<?> 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.
- Returns:
- the next submission, or
null if none
-
drainTasksTo
protected int drainTasksTo(Collection<? super ForkJoinTask<?>> 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 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.
- Parameters:
c - the collection to transfer elements into
- Returns:
- the number of elements transferred
-
toString
public 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.
- Overrides:
toString in class Object
- Returns:
- a string identifying this pool, as well as its state
-
shutdown
public 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 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.
- Throws:
SecurityException - if a security manager exists and
the caller is not permitted to modify threads
because it does not hold RuntimePermission("modifyThread")
-
newTaskFor
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable,
T value)
Returns a RunnableFuture for the given runnable and default
value.
- Overrides:
newTaskFor in class AbstractExecutorService
- Type Parameters:
T - the type of the given value
- Parameters:
runnable - the runnable task being wrapped
value - the default value for the returned future
- Returns:
- a
RunnableFuture which, when run, will run the
underlying runnable and which, as a Future, will yield
the given value as its result and provide for cancellation of
the underlying task
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|