Interface Future<T>
- Type Parameters:
T- type of the result of the computation
When the computation is still running, we say that the future is pending. Once the computation finishes, we say that the future is complete. Alternatively, after successful cancellation, we say the future is cancelled. The computation may finish with two kinds of outcomes: success, producing a value, and failure, producing an error.
To obtain the result, consumers of the future are expected to register
a completion callback using Future.then().
The callback is called with a pair [value, error] once the future completes.
To distinguish a sucessful outcome from a failure, the error should be
tested. If the error is null, the outcome is successful. Note that
a success may still produce a null value.
The callback may be registered before or after the future completes, and is guaranteed to be called exactly once. The thread on which the callback is called is not specified; it may be the thread that completes the future or the thread that registers the callback. Only one callback may be registered; attempts to register a second callback end up with an exception.
Future objects are created by a Completer. The Completer is
also the only way through which the future may be completed. For convenience,
static factory methods are provided to construct already complete futures:
Future.of(), Future.ofError(),
and Future.from().
A future consumer may request cancellation of the computation by calling
Future.cancel(). This is only possible while the future is
pending; when the future is already complete, this is a noop.
Unlike common Future abstractions, this one is fairly limited.
There may only be one completion callback, and there are no combinators
such as map or flatMap.
-
Method Summary
Modifier and TypeMethodDescriptionBlocks the calling thread until this future is complete or cancelled, and then returns the value of this future or throws the error, or throwsCancellationException.voidcancel()Requests cancellation of the computation represented by this future.static <T> Future<T> Returns a future that is already complete with the outcome of givencallable(which may be a returned value or a thrown error).booleanReturns whether this future is cancelled.booleanReturns whether this future is complete.static <T> Future<T> of(T value) Returns a future that is already complete with givenvalue.static <T> Future<T> Returns a future that is already complete with givenerror.voidthen(BiConsumer<T, Throwable> callback) Registers a completion callback with this future.voidthenComplete(Completer<T> completer) Registers a completion callback with this future.
-
Method Details
-
of
Returns a future that is already complete with givenvalue.- Type Parameters:
T- type of the value- Parameters:
value- the value; may benull- Returns:
- the future that is already complete with the value; never
null
-
ofError
Returns a future that is already complete with givenerror.- Type Parameters:
T- type of hypothetical result; only for type inference- Parameters:
error- the error; must not benull- Returns:
- the future that is already complete with the error; never
null
-
from
Returns a future that is already complete with the outcome of givencallable(which may be a returned value or a thrown error).- Type Parameters:
T- type of the result of givencallable- Parameters:
callable- the callable to call; must not benull- Returns:
- the future that is complete with the outcome of the
callable; nevernull
-
then
Registers a completion callback with this future. The first argument of theBiConsumeris the value of the future, the second argument is the error.Value may be
nullin case of a success, but error is nevernullin case of a failure. Therefore, idiomatic usage looks like:future.then((value, error) -> { if (error == null) { ... use value ... } else { ... use error ... } });- Parameters:
callback- the completion callback to be registered; must not benull
-
thenComplete
Registers a completion callback with this future. The callback forwards the result of this future into the given completer.- Parameters:
completer- the completer to which the result of this future is forwarded; must not benull
-
isComplete
boolean isComplete()Returns whether this future is complete.- Returns:
trueif this future is complete,falseotherwise
-
isCancelled
boolean isCancelled()Returns whether this future is cancelled.- Returns:
trueif this future is cancelled,falseotherwise
-
awaitBlocking
Blocks the calling thread until this future is complete or cancelled, and then returns the value of this future or throws the error, or throwsCancellationException. In case this future is already complete or cancelled when this method is called, no blocking occurs.The blocked thread may be interrupted, in which case this method throws
InterruptedException.This method should rarely be used without previous checking with
isComplete()orisCancelled().- Returns:
- the value of this future; may be
null - Throws:
Throwable- the error of this future,CancellationExceptionorInterruptedException
-
cancel
void cancel()Requests cancellation of the computation represented by this future.- See Also:
-