get
methods
Future
, with methods to start and cancel
a computation, query to see if the computation is complete, and
retrieve the result of the computation. The result can only be
retrieved when the computation has completed; the get
methods will block if the computation has not yet completed. Once
the computation has completed, the computation cannot be restarted
or cancelled (unless the computation is invoked using
runAndReset
).
A FutureTask
can be used to wrap a Callable
or
Runnable
object. Because FutureTask
implements
Runnable
, a FutureTask
can be submitted to an
Executor
for execution.
In addition to serving as a standalone class, this class provides
protected
functionality that may be useful when creating
customized task classes.
Modifier and Type | Class and Description |
---|---|
pack-priv static class | FutureTask.
Simple linked list nodes to record waiting threads in a Treiber stack. |
Modifier and Type | Field and Description |
---|---|
private Callable | callable
The underlying callable; nulled out after running |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private Object | outcome
The result to return or exception to throw from get() |
private volatile Thread | runner
The thread running the callable; CASed during run() |
private static final VarHandle | |
private volatile int | state
The run state of this task, initially NEW. |
private static final VarHandle | |
private volatile FutureTask. | waiters
Treiber stack of waiting threads |
private static final VarHandle |
Access | Constructor and Description |
---|---|
public | FutureTask(Callable<V>
the callable task callable)Creates a |
public | FutureTask(Runnable
the runnable task runnable, V the result to return on successful completion. If
you don't need a particular result, consider using
constructions of the form:
result)Future<?> f = new FutureTask<Void>(runnable, null) Creates a |
Modifier and Type | Method and Description |
---|---|
private int | Returns: state upon completion or at timeouttrue if use timed waits timed, long time to wait, if timed nanos)Awaits completion or aborts on interrupt or timeout. |
public boolean | cancel(boolean
true if the thread
executing this task should be interrupted (if the thread is
known to the implementation); otherwise, in-progress tasks are
allowed to completeImplements java. Attempts to cancel execution of this task. |
protected void | done()
Protected method invoked when this task transitions to state
|
public Throwable | exceptionNow()
Overrides default java. Returns the exception thrown by the task, without waiting. |
private void | |
public V | get()
Implements java. Waits if necessary for the computation to complete, and then retrieves its result. |
public V | get(long
the maximum time to wait timeout, TimeUnit the time unit of the timeout argument unit)Implements java. Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available. |
private void | handlePossibleCancellationInterrupt(int s)
Ensures that any interrupt from a possible cancel(true) is only delivered to a task while in run or runAndReset. |
public boolean | isCancelled()
Implements java. Returns |
public boolean | |
private void | removeWaiter(FutureTask.
Tries to unlink a timed-out or interrupted wait node to avoid accumulating garbage. |
private V | |
public V | resultNow()
Overrides default java. Returns the computed result, without waiting. |
public void | run()
Implements java. Sets this Future to the result of its computation unless it has been cancelled. |
protected boolean | Returns: true if successfully run and resetExecutes the computation without setting its result, and then resets this future to initial state, failing to do so if the computation encounters an exception or is cancelled. |
protected void | set(V
the value v)Sets the result of this future to the given value unless this future has already been set or has been cancelled. |
protected void | setException(Throwable
the cause of failure t)Causes this future to report an |
public Future. | |
public String | Returns: a string representation of this FutureTaskOverrides java. Returns a string representation of this FutureTask. |
callable | back to summary |
---|---|
private Callable<V> callable The underlying callable; nulled out after running |
CANCELLED | back to summary |
---|---|
private static final int CANCELLED |
COMPLETING | back to summary |
---|---|
private static final int COMPLETING |
EXCEPTIONAL | back to summary |
---|---|
private static final int EXCEPTIONAL |
INTERRUPTED | back to summary |
---|---|
private static final int INTERRUPTED |
INTERRUPTING | back to summary |
---|---|
private static final int INTERRUPTING |
NEW | back to summary |
---|---|
private static final int NEW |
NORMAL | back to summary |
---|---|
private static final int NORMAL |
outcome | back to summary |
---|---|
private Object outcome The result to return or exception to throw from get() |
runner | back to summary |
---|---|
private volatile Thread runner The thread running the callable; CASed during run() |
RUNNER | back to summary |
---|---|
private static final VarHandle RUNNER |
state | back to summary |
---|---|
private volatile int state The run state of this task, initially NEW. The run state transitions to a terminal state only in methods set, setException, and cancel. During completion, state may take on transient values of COMPLETING (while outcome is being set) or INTERRUPTING (only while interrupting the runner to satisfy a cancel(true)). Transitions from these intermediate to final states use cheaper ordered/lazy writes because values are unique and cannot be further modified. Possible state transitions: NEW -> COMPLETING -> NORMAL NEW -> COMPLETING -> EXCEPTIONAL NEW -> CANCELLED NEW -> INTERRUPTING -> INTERRUPTED |
STATE | back to summary |
---|---|
private static final VarHandle STATE |
waiters | back to summary |
---|---|
private volatile FutureTask. Treiber stack of waiting threads |
WAITERS | back to summary |
---|---|
private static final VarHandle WAITERS |
FutureTask | back to summary |
---|---|
public FutureTask(Callable<V> callable) Creates a
|
FutureTask | back to summary |
---|---|
public FutureTask(Runnable runnable, V result) Creates a
|
awaitDone | back to summary |
---|---|
private int awaitDone(boolean timed, long nanos) throws InterruptedException Awaits completion or aborts on interrupt or timeout.
|
cancel | back to summary |
---|---|
public boolean cancel(boolean mayInterruptIfRunning) Implements java. Doc from java. Attempts to cancel execution of this task. This method has no
effect if the task is already completed or cancelled, or could
not be cancelled for some other reason. Otherwise, if this
task has not started when The return value from this method does not necessarily
indicate whether the task is now cancelled; use
|
done | back to summary |
---|---|
protected void done() Protected method invoked when this task transitions to state
|
exceptionNow | back to summary |
---|---|
public Throwable exceptionNow() Overrides default java. Doc from java. Returns the exception thrown by the task, without waiting. This method is for cases where the caller knows that the task has already completed with an exception. |
finishCompletion | back to summary |
---|---|
private void finishCompletion() Removes and signals all waiting threads, invokes done(), and nulls out callable. |
get | back to summary |
---|---|
public V get() throws InterruptedException, ExecutionException Implements java. Doc from java. Waits if necessary for the computation to complete, and then retrieves its result.
|
get | back to summary |
---|---|
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Implements java. Doc from java. Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
|
handlePossibleCancellationInterrupt | back to summary |
---|---|
private void handlePossibleCancellationInterrupt(int s) Ensures that any interrupt from a possible cancel(true) is only delivered to a task while in run or runAndReset. |
isCancelled | back to summary |
---|---|
public boolean isCancelled() Implements java. Doc from java. Returns
|
isDone | back to summary |
---|---|
public boolean isDone() Implements java. Doc from java. Returns
|
removeWaiter | back to summary |
---|---|
private void removeWaiter(FutureTask. Tries to unlink a timed-out or interrupted wait node to avoid accumulating garbage. Internal nodes are simply unspliced without CAS since it is harmless if they are traversed anyway by releasers. To avoid effects of unsplicing from already removed nodes, the list is retraversed in case of an apparent race. This is slow when there are a lot of nodes, but we don't expect lists to be long enough to outweigh higher-overhead schemes. |
report | back to summary |
---|---|
private V report(int s) throws ExecutionException Returns result or throws exception for completed task.
|
resultNow | back to summary |
---|---|
public V resultNow() Overrides default java. Doc from java. Returns the computed result, without waiting. This method is for cases where the caller knows that the task has already completed successfully, for example when filtering a stream of Future objects for the successful tasks and using a mapping operation to obtain a stream of results. results = futures.stream() .filter(f -> f.state() == Future.State.SUCCESS) .map(Future::resultNow) .toList();
|
run | back to summary |
---|---|
public void run() Implements java. Doc from java. Sets this Future to the result of its computation unless it has been cancelled. |
runAndReset | back to summary |
---|---|
protected boolean runAndReset() Executes the computation without setting its result, and then resets this future to initial state, failing to do so if the computation encounters an exception or is cancelled. This is designed for use with tasks that intrinsically execute more than once.
|
set | back to summary |
---|---|
protected void set(V v) Sets the result of this future to the given value unless this future has already been set or has been cancelled. This method is invoked internally by the
|
setException | back to summary |
---|---|
protected void setException(Throwable t) Causes this future to report an This method is invoked internally by the
|
state | back to summary |
---|---|
public Future. Overrides default java. Doc from java. Returns the computation state. |
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a string representation of this FutureTask. Implementation Specification The default implementation returns a string identifying this
FutureTask, as well as its completion state. The state, in
brackets, contains one of the strings
|
Modifier and Type | Field and Description |
---|---|
pack-priv volatile FutureTask. | |
pack-priv volatile Thread |
Access | Constructor and Description |
---|---|
pack-priv |
next | back to summary |
---|---|
pack-priv volatile FutureTask. |
thread | back to summary |
---|---|
pack-priv volatile Thread thread |
WaitNode | back to summary |
---|---|
pack-priv WaitNode() |