FutureTask

A cancellable asynchronous computation. This class provides a base implementation of {@link 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 {@code 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 {@link #runAndReset}).

<p>A {@code FutureTask} can be used to wrap a {@link Callable} or {@link Runnable} object. Because {@code FutureTask} implements {@code Runnable}, a {@code FutureTask} can be submitted to an {@link Executor} for execution.

<p>In addition to serving as a standalone class, this class provides {@code protected} functionality that may be useful when creating customized task classes.

@author Doug Lea @param (V) The result type returned by this FutureTask's {@code get} methods

Constructors

this
this(Callable!(V) callable)

Creates a {@code FutureTask} that will, upon running, execute the given {@code Callable}.

this
this(Runnable runnable)

Creates a {@code FutureTask} that will, upon running, execute the given {@code Runnable}, and arrange that {@code get} will return the given result on successful completion.

this
this(Runnable runnable, V result)
Undocumented in source.

Members

Classes

WaitNode
class WaitNode

Simple linked list nodes to record waiting threads in a Treiber stack. See other classes such as Phaser and SynchronousQueue for more detailed explanation.

Functions

cancel
bool cancel(bool mayInterruptIfRunning)
Undocumented in source. Be warned that the author may not have intended to support it.
done
void done()

Protected method invoked when this task transitions to state {@code isDone} (whether normally or via cancellation). The default implementation does nothing. Subclasses may override this method to invoke completion callbacks or perform bookkeeping. Note that you can query status inside the implementation of this method to determine whether this task has been cancelled.

get
V get()

@throws CancellationException {@inheritDoc}

get
V get(Duration timeout)

@throws CancellationException {@inheritDoc}

isCancelled
bool isCancelled()
Undocumented in source. Be warned that the author may not have intended to support it.
isDone
bool isDone()
Undocumented in source. Be warned that the author may not have intended to support it.
run
void run()

Sets the result of this future to the given value unless this future has already been set or has been cancelled.

run
void run()
Undocumented in source. Be warned that the author may not have intended to support it.
runAndReset
bool 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
void set()

Sets the result of this future to the given value unless this future has already been set or has been cancelled.

set
void set(V v)
Undocumented in source. Be warned that the author may not have intended to support it.
setException
void setException(Throwable t)

Causes this future to report an {@link ExecutionException} with the given throwable as its cause, unless this future has already been set or has been cancelled.

toString
string toString()

Returns a string representation of this FutureTask.

Meta