GeezerVison is showing docs.oracle.com — ForkJoinTask (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util.concurrent
Class ForkJoinTask<V>
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
| Modifier and Type |
Method |
Description |
static <T> ForkJoinTask<T> |
adapt(Callable<? extends T> callable) |
Returns a new ForkJoinTask that performs the call
method of the given Callable as its action, and returns
its result upon join(), translating any checked exceptions
encountered into RuntimeException.
|
static ForkJoinTask<?> |
adapt(Runnable runnable) |
Returns a new ForkJoinTask that performs the run
method of the given Runnable as its action, and returns
a null result upon join().
|
static <T> ForkJoinTask<T> |
adapt(Runnable runnable,
T result) |
Returns a new ForkJoinTask that performs the run
method of the given Runnable as its action, and returns
the given result upon join().
|
boolean |
cancel(boolean mayInterruptIfRunning) |
Attempts to cancel execution of this task.
|
boolean |
compareAndSetForkJoinTaskTag(short e,
short tag) |
Atomically conditionally sets the tag value for this task.
|
void |
complete(V value) |
Completes this task, and if not already aborted or cancelled,
returning the given value as the result of subsequent
invocations of join and related operations.
|
void |
completeExceptionally(Throwable ex) |
Completes this task abnormally, and if not already aborted or
cancelled, causes it to throw the given exception upon
join and related operations.
|
protected abstract boolean |
exec() |
Immediately performs the base action of this task and returns
true if, upon return from this method, this task is guaranteed
to have completed normally.
|
ForkJoinTask<V> |
fork() |
|
V |
get() |
Waits if necessary for the computation to complete, and then
retrieves its result.
|
V |
get(long timeout,
TimeUnit unit) |
Waits if necessary for at most the given time for the computation
to complete, and then retrieves its result, if available.
|
Throwable |
getException() |
Returns the exception thrown by the base computation, or a
CancellationException if cancelled, or null if
none or if the method has not yet completed.
|
short |
getForkJoinTaskTag() |
Returns the tag for this task.
|
static ForkJoinPool |
getPool() |
Returns the pool hosting the current task execution, or null
if this task is executing outside of any ForkJoinPool.
|
static int |
getQueuedTaskCount() |
Returns an estimate of the number of tasks that have been
forked by the current worker thread but not yet executed.
|
abstract V |
getRawResult() |
Returns the result that would be returned by join(), even
if this task completed abnormally, or null if this task
is not known to have been completed.
|
static int |
getSurplusQueuedTaskCount() |
Returns an estimate of how many more locally queued tasks are
held by the current worker thread than there are other worker
threads that might steal them, or zero if this thread is not
operating in a ForkJoinPool.
|
static void |
helpQuiesce() |
Possibly executes tasks until the pool hosting the current task
is quiescent.
|
static boolean |
inForkJoinPool() |
Returns true if the current thread is a ForkJoinWorkerThread executing as a ForkJoinPool computation.
|
V |
invoke() |
Commences performing this task, awaits its completion if
necessary, and returns its result, or throws an (unchecked)
RuntimeException or Error if the underlying
computation did so.
|
static <T extends ForkJoinTask<?>> Collection<T> |
invokeAll(Collection<T> tasks) |
Forks all tasks in the specified collection, returning when
isDone holds for each task or an (unchecked) exception
is encountered, in which case the exception is rethrown.
|
static void |
invokeAll(ForkJoinTask<?>... tasks) |
Forks the given tasks, returning when isDone holds for
each task or an (unchecked) exception is encountered, in which
case the exception is rethrown.
|
static void |
invokeAll(ForkJoinTask<?> t1,
ForkJoinTask<?> t2) |
Forks the given tasks, returning when isDone holds for
each task or an (unchecked) exception is encountered, in which
case the exception is rethrown.
|
boolean |
isCancelled() |
Returns true if this task was cancelled before it completed
normally.
|
boolean |
isCompletedAbnormally() |
Returns true if this task threw an exception or was cancelled.
|
boolean |
isCompletedNormally() |
Returns true if this task completed without throwing an
exception and was not cancelled.
|
boolean |
isDone() |
Returns true if this task completed.
|
V |
join() |
Returns the result of the computation when it is
done.
|
protected static ForkJoinTask<?> |
peekNextLocalTask() |
Returns, but does not unschedule or execute, a task queued by
the current thread but not yet executed, if one is immediately
available.
|
protected static ForkJoinTask<?> |
pollNextLocalTask() |
Unschedules and returns, without executing, the next task
queued by the current thread but not yet executed, if the
current thread is operating in a ForkJoinPool.
|
protected static ForkJoinTask<?> |
pollTask() |
If the current thread is operating in a ForkJoinPool,
unschedules and returns, without executing, the next task
queued by the current thread but not yet executed, if one is
available, or if not available, a task that was forked by some
other thread, if available.
|
void |
quietlyComplete() |
Completes this task normally without setting a value.
|
void |
quietlyInvoke() |
Commences performing this task and awaits its completion if
necessary, without returning its result or throwing its
exception.
|
void |
quietlyJoin() |
Joins this task, without returning its result or throwing its
exception.
|
void |
reinitialize() |
Resets the internal bookkeeping state of this task, allowing a
subsequent fork.
|
short |
setForkJoinTaskTag(short tag) |
Atomically sets the tag value for this task.
|
protected abstract void |
setRawResult(V value) |
Forces the given value to be returned as a result.
|
boolean |
tryUnfork() |
Tries to unschedule this task for execution.
|
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
ForkJoinTask
public ForkJoinTask()
-
Method Detail
-
join
public final V join()
Returns the result of the computation when it is
done. This method differs from get() in that
abnormal completion results in RuntimeException or
Error, not ExecutionException, and that
interrupts of the calling thread do not cause the
method to abruptly return by throwing InterruptedException.
- Returns:
- the computed result
-
invoke
public final V invoke()
Commences performing this task, awaits its completion if
necessary, and returns its result, or throws an (unchecked)
RuntimeException or Error if the underlying
computation did so.
- Returns:
- the computed result
-
isDone
public final boolean isDone()
Description copied from interface: Future
Returns true if this task completed.
Completion may be due to normal termination, an exception, or
cancellation -- in all of these cases, this method will return
true.
- Specified by:
isDone in interface Future<V>
- Returns:
true if this task completed
-
getException
public final Throwable getException()
Returns the exception thrown by the base computation, or a
CancellationException if cancelled, or null if
none or if the method has not yet completed.
- Returns:
- the exception, or
null if none
-
completeExceptionally
public void completeExceptionally(Throwable ex)
Completes this task abnormally, and if not already aborted or
cancelled, causes it to throw the given exception upon
join and related operations. This method may be used
to induce exceptions in asynchronous tasks, or to force
completion of tasks that would not otherwise complete. Its use
in other situations is discouraged. This method is
overridable, but overridden versions must invoke super
implementation to maintain guarantees.
- Parameters:
ex - the exception to throw. If this exception is not a
RuntimeException or Error, the actual exception
thrown will be a RuntimeException with cause ex.
-
complete
public void complete(V value)
Completes this task, and if not already aborted or cancelled,
returning the given value as the result of subsequent
invocations of join and related operations. This method
may be used to provide results for asynchronous tasks, or to
provide alternative handling for tasks that would not otherwise
complete normally. Its use in other situations is
discouraged. This method is overridable, but overridden
versions must invoke super implementation to maintain
guarantees.
- Parameters:
value - the result value for this task
-
quietlyJoin
public final void quietlyJoin()
Joins this task, without returning its result or throwing its
exception. This method may be useful when processing
collections of tasks when some have been cancelled or otherwise
known to have aborted.
-
quietlyInvoke
public final void quietlyInvoke()
Commences performing this task and awaits its completion if
necessary, without returning its result or throwing its
exception.
-
getPool
public static ForkJoinPool getPool()
Returns the pool hosting the current task execution, or null
if this task is executing outside of any ForkJoinPool.
- Returns:
- the pool, or
null if none
- See Also:
inForkJoinPool()
-
tryUnfork
public boolean tryUnfork()
Tries to unschedule this task for execution. This method will
typically (but is not guaranteed to) succeed if this task is
the most recently forked task by the current thread, and has
not commenced executing in another thread. This method may be
useful when arranging alternative local processing of tasks
that could have been, but were not, stolen.
- Returns:
true if unforked
-
getQueuedTaskCount
public static int getQueuedTaskCount()
Returns an estimate of the number of tasks that have been
forked by the current worker thread but not yet executed. This
value may be useful for heuristic decisions about whether to
fork other tasks.
- Returns:
- the number of tasks
-
getSurplusQueuedTaskCount
public static int getSurplusQueuedTaskCount()
Returns an estimate of how many more locally queued tasks are
held by the current worker thread than there are other worker
threads that might steal them, or zero if this thread is not
operating in a ForkJoinPool. This value may be useful for
heuristic decisions about whether to fork other tasks. In many
usages of ForkJoinTasks, at steady state, each worker should
aim to maintain a small constant surplus (for example, 3) of
tasks, and to process computations locally if this threshold is
exceeded.
- Returns:
- the surplus number of tasks, which may be negative
-
setRawResult
protected abstract void setRawResult(V value)
Forces the given value to be returned as a result. This method
is designed to support extensions, and should not in general be
called otherwise.
- Parameters:
value - the value
-
exec
protected abstract boolean exec()
Immediately performs the base action of this task and returns
true if, upon return from this method, this task is guaranteed
to have completed normally. This method may return false
otherwise, to indicate that this task is not necessarily
complete (or is not known to be complete), for example in
asynchronous actions that require explicit invocations of
completion methods. This method may also throw an (unchecked)
exception to indicate abnormal exit. This method is designed to
support extensions, and should not in general be called
otherwise.
- Returns:
true if this task is known to have completed normally
-
peekNextLocalTask
protected static ForkJoinTask<?> peekNextLocalTask()
Returns, but does not unschedule or execute, a task queued by
the current thread but not yet executed, if one is immediately
available. There is no guarantee that this task will actually
be polled or executed next. Conversely, this method may return
null even if a task exists but cannot be accessed without
contention with other threads. This method is designed
primarily to support extensions, and is unlikely to be useful
otherwise.
- Returns:
- the next task, or
null if none are available
-
pollNextLocalTask
protected static ForkJoinTask<?> pollNextLocalTask()
Unschedules and returns, without executing, the next task
queued by the current thread but not yet executed, if the
current thread is operating in a ForkJoinPool. This method is
designed primarily to support extensions, and is unlikely to be
useful otherwise.
- Returns:
- the next task, or
null if none are available
-
pollTask
protected static ForkJoinTask<?> pollTask()
If the current thread is operating in a ForkJoinPool,
unschedules and returns, without executing, the next task
queued by the current thread but not yet executed, if one is
available, or if not available, a task that was forked by some
other thread, if available. Availability may be transient, so a
null result does not necessarily imply quiescence of
the pool this task is operating in. This method is designed
primarily to support extensions, and is unlikely to be useful
otherwise.
- Returns:
- a task, or
null if none are available
-
compareAndSetForkJoinTaskTag
public final boolean compareAndSetForkJoinTaskTag(short e,
short tag)
Atomically conditionally sets the tag value for this task.
Among other applications, tags can be used as visit markers
in tasks operating on graphs, as in methods that check: if (task.compareAndSetForkJoinTaskTag((short)0, (short)1))
before processing, otherwise exiting because the node has
already been visited.
- Parameters:
e - the expected tag value
tag - the new tag value
- Returns:
true if successful; i.e., the current value was
equal to e and is now tag.
- Since:
- 1.8
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.
|