CompletableFuture

A {@link Future} that may be explicitly completed (setting its value and status), and may be used as a {@link CompletionStage}, supporting dependent functions and actions that trigger upon its completion.

<p>When two or more threads attempt to {@link #complete complete}, {@link #completeExceptionally completeExceptionally}, or {@link #cancel cancel} a CompletableFuture, only one of them succeeds.

<p>In addition to these and related methods for directly manipulating status and results, CompletableFuture implements interface {@link CompletionStage} with the following policies: <ul>

<li>Actions supplied for dependent completions of <em>non-async</em> methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method.

<li>All <em>async</em> methods without an explicit Executor argument are performed using the {@link ForkJoinPool#commonPool()} (unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run each task). This may be overridden for non-static methods in subclasses by defining method {@link #defaultExecutor()}. To simplify monitoring, debugging, and tracking, all generated asynchronous tasks are instances of the marker interface {@link AsynchronousCompletionTask}. Operations with time-delays can use adapter methods defined in this class, for

class CompletableFuture : AbstractCompletableFuture , Future!(T), Promise!(T), CompletionStage!(T)(
T
) {}

Constructors

this
this(bool completed)
Undocumented in source.
this
this(T r)

Creates a new complete CompletableFuture with given encoded result.

this
this()

Creates a new incomplete CompletableFuture.

this
this(AltResult r)
Undocumented in source.

Members

Aliases

BiConsumerT
alias BiConsumerT = Action1!(Throwable)
Undocumented in source.
BiConsumerT
alias BiConsumerT = Action2!(T, Throwable)
Undocumented in source.
ConsumerT
alias ConsumerT = Action
Undocumented in source.
ConsumerT
alias ConsumerT = Consumer!(T)
Undocumented in source.
FunctionT
alias FunctionT(V) = Func!(V)
Undocumented in source.
FunctionT
alias FunctionT(V) = Func1!(T, V)
Undocumented in source.

Functions

acceptEither
CompletableFuture!(void) acceptEither(CompletionStage!(T) other, ConsumerT action)
Undocumented in source. Be warned that the author may not have intended to support it.
acceptEitherAsync
CompletableFuture!(void) acceptEitherAsync(CompletionStage!(T) other, ConsumerT action)
Undocumented in source. Be warned that the author may not have intended to support it.
acceptEitherAsync
CompletableFuture!(void) acceptEitherAsync(CompletionStage!(T) other, ConsumerT action, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
applyToEither
CompletableFuture!(U) applyToEither(CompletionStage!(T) other, FunctionT!(U) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
applyToEitherAsync
CompletableFuture!(U) applyToEitherAsync(CompletionStage!(T) other, FunctionT!(U) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
applyToEitherAsync
CompletableFuture!(U) applyToEitherAsync(CompletionStage!(T) other, FunctionT!(U) fn, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
biAccept
bool biAccept(CompletableFuture!R r, CompletableFuture!S s, BiConsumer!(R, S) f, BiAccept!(R, S) c)
Undocumented in source. Be warned that the author may not have intended to support it.
biApply
bool biApply(CompletableFuture!R r, CompletableFuture!S s, BiFunction!(R, S, T) f, BiApply!(R, S, T) c)
Undocumented in source. Be warned that the author may not have intended to support it.
biRun
bool biRun(AbstractCompletableFuture r, AbstractCompletableFuture s, Runnable f, IUniCompletion c)
Undocumented in source. Be warned that the author may not have intended to support it.
bipush
void bipush(AbstractCompletableFuture b, BiCompletion c)

Pushes completion to this and b unless both done. Caller should first check that either result or b.result is null.

cancel
bool cancel(bool mayInterruptIfRunning)

If not already completed, completes this CompletableFuture with a {@link CancellationException}. Dependent CompletableFutures that have not already completed will also complete exceptionally, with a {@link CompletionException} caused by this {@code CancellationException}.

cleanStack
void cleanStack()

Traverses stack and unlinks one or more dead Completions, if found.

complete
bool complete()
Undocumented in source. Be warned that the author may not have intended to support it.
complete
bool complete(T value)

If not already completed, sets the value returned by {@link #get()} and related methods to the given value.

completeAsync
CompletableFuture!(T) completeAsync(Supplier!(T) supplier, Executor executor)

Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the given executor.

completeAsync
CompletableFuture!(T) completeAsync(Supplier!(T) supplier)

Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the default executor.

completeExceptionally
bool completeExceptionally(Throwable ex)

If not already completed, causes invocations of {@link #get()} and related methods to throw the given exception.

completeNull
bool completeNull()

Completes with the null value, unless already completed.

completeOnTimeout
CompletableFuture!(T) completeOnTimeout(Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.
completeOnTimeout
CompletableFuture!(T) completeOnTimeout(T value, Duration timeout)

Completes this CompletableFuture with the given value if not otherwise completed before the given timeout.

completeValue
bool completeValue()

Completes with a non-exceptional result, unless already completed.

completeValue
bool completeValue(T t)
Undocumented in source. Be warned that the author may not have intended to support it.
completeValue
bool completeValue(AltResult r)
Undocumented in source. Be warned that the author may not have intended to support it.
copy
CompletableFuture!(T) copy()

Returns a new CompletableFuture that is completed normally with the same value as this CompletableFuture when it completes normally. If this CompletableFuture completes exceptionally, then the returned CompletableFuture completes exceptionally with a CompletionException with this exception as cause. The behavior is equivalent to {@code thenApply(x -> x)}. This method may be useful as a form of "defensive copying", to prevent clients from completing, while still being able to arrange dependent actions.

defaultExecutor
Executor defaultExecutor()

Returns the default Executor used for async methods that do not specify an Executor. This class uses the {@link ForkJoinPool#commonPool()} if it supports more than one parallel thread, or else an Executor using one thread per async task. This method may be overridden in subclasses to return an Executor that provides at least one independent thread.

exceptionally
CompletableFuture!(T) exceptionally(Function!(Throwable, T) fn)

Returns a new CompletableFuture that is completed when this CompletableFuture completes, with the result of the given function of the exception triggering this CompletableFuture's completion when it completes exceptionally; otherwise, if this CompletableFuture completes normally, then the returned CompletableFuture also completes normally with the same value. Note: More flexible versions of this functionality are available using methods {@code whenComplete} and {@code handle}.

failed
void failed(Exception x)

Callback invoked when the operation fails.

get
void get()
Undocumented in source. Be warned that the author may not have intended to support it.
get
void get(Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.
get
T get()

Waits if necessary for this future to complete, and then returns its result.

get
T get(Duration timeout)

Waits if necessary for at most the given time for this future to complete, and then returns its result, if available.

getNow
void getNow()
Undocumented in source. Be warned that the author may not have intended to support it.
getNow
T getNow(T valueIfAbsent)

Returns the result value (or throws any encountered exception) if completed, else returns the given valueIfAbsent.

getNumberOfDependents
int getNumberOfDependents()

Returns the estimated number of CompletableFutures whose completions are awaiting completion of this CompletableFuture. This method is designed for use in monitoring system state, not for synchronization control.

handle
CompletableFuture!(U) handle(BiFunction!(T, Throwable, U) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
handleAsync
CompletableFuture!(U) handleAsync(BiFunction!(T, Throwable, U) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
handleAsync
CompletableFuture!(U) handleAsync(BiFunction!(T, Throwable, U) fn, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
join
void join()
Undocumented in source. Be warned that the author may not have intended to support it.
join
T join()

Returns the result value when complete, or throws an (unchecked) exception if completed exceptionally. To better conform with the use of common functional forms, if a computation involved in the completion of this CompletableFuture threw an exception, this method throws an (unchecked) {@link CompletionException} with the underlying exception as its cause.

minimalCompletionStage
CompletionStage!(T) minimalCompletionStage()

Returns a new CompletionStage that is completed normally with the same value as this CompletableFuture when it completes normally, and cannot be independently completed or otherwise used in ways not defined by the methods of interface {@link CompletionStage}. If this CompletableFuture completes exceptionally, then the returned CompletionStage completes exceptionally with a CompletionException with this exception as cause.

obtrudeException
void obtrudeException(Throwable ex)

Forcibly causes subsequent invocations of method {@link #get()} and related methods to throw the given exception, whether or not already completed. This method is designed for use only in error recovery actions, and even in such situations may result in ongoing dependent completions using established versus overwritten outcomes.

obtrudeValue
void obtrudeValue()
Undocumented in source. Be warned that the author may not have intended to support it.
obtrudeValue
void obtrudeValue(T value)

Forcibly sets or resets the value subsequently returned by method {@link #get()} and related methods, whether or not already completed. This method is designed for use only in error recovery actions, and even in such situations may result in ongoing dependent completions using established versus overwritten outcomes.

orTimeout
CompletableFuture!(T) orTimeout(Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.
orTimeout
CompletableFuture!(T) orTimeout(Duration timeout)

Exceptionally completes this CompletableFuture with a {@link TimeoutException} if not otherwise completed before the given timeout.

orpush
void orpush(AbstractCompletableFuture b, BiCompletion c)

Pushes completion to this and b unless either done. Caller should first check that result and b.result are both null.

postComplete
void postComplete()

Pops and tries to trigger all reachable dependents. Call only when known to be done.

postFire
CompletableFuture!(T) postFire(AbstractCompletableFuture a, AbstractCompletableFuture b, int mode)

Post-processing after successful BiCompletion tryFire.

pushStack
void pushStack(Completion c)

Unconditionally pushes c onto stack, retrying if necessary.

runAfterBoth
CompletableFuture!(void) runAfterBoth(CompletionStage!U other, Action action)
Undocumented in source. Be warned that the author may not have intended to support it.
runAfterBoth
CompletableFuture!(void) runAfterBoth(CompletionStage!U other, Runnable action)
Undocumented in source. Be warned that the author may not have intended to support it.
runAfterBothAsync
CompletableFuture!(void) runAfterBothAsync(CompletionStage!U other, Runnable action)
Undocumented in source. Be warned that the author may not have intended to support it.
runAfterBothAsync
CompletableFuture!(void) runAfterBothAsync(CompletionStage!U other, Runnable action, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
runAfterEither
CompletableFuture!(void) runAfterEither(CompletionStag!U other, Runnable action)
Undocumented in source. Be warned that the author may not have intended to support it.
runAfterEitherAsync
CompletableFuture!(void) runAfterEitherAsync(CompletionStage!U other, Runnable action)
Undocumented in source. Be warned that the author may not have intended to support it.
runAfterEitherAsync
CompletableFuture!(void) runAfterEitherAsync(CompletionStage!U other, Runnable action, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
succeeded
void succeeded()

Callback invoked when the operation completes.

succeeded
void succeeded(T result)

Callback invoked when the operation completes.

thenAccept
CompletableFuture!(void) thenAccept(ConsumerT action)
Undocumented in source. Be warned that the author may not have intended to support it.
thenAcceptAsync
CompletableFuture!(void) thenAcceptAsync(ConsumerT action)
Undocumented in source. Be warned that the author may not have intended to support it.
thenAcceptAsync
CompletableFuture!(void) thenAcceptAsync(ConsumerT action, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
thenAcceptBoth
CompletableFuture!(void) thenAcceptBoth(CompletionStage!(U) other, BiConsumer!(T, U) action)
Undocumented in source. Be warned that the author may not have intended to support it.
thenAcceptBothAsync
CompletableFuture!(void) thenAcceptBothAsync(CompletionStage!(U) other, BiConsumer!(T, U) action)
Undocumented in source. Be warned that the author may not have intended to support it.
thenAcceptBothAsync
CompletableFuture!(void) thenAcceptBothAsync(CompletionStage!(U) other, BiConsumer!(T, U) action, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
thenApply
CompletableFuture!(U) thenApply(FunctionT!(U) fn)
thenApplyAsync
CompletableFuture!(U) thenApplyAsync(FunctionT!(U) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
thenApplyAsync
CompletableFuture!(U) thenApplyAsync(FunctionT!(U) fn, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
thenCombine
CompletableFuture!(V) thenCombine(CompletionStage!(U) other, BiFunction!(T, U, V) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
thenCombineAsync
CompletableFuture!(V) thenCombineAsync(CompletionStage!(U) other, BiFunction!(T, U, V) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
thenCombineAsync
CompletableFuture!(V) thenCombineAsync(CompletionStage!(U) other, BiFunction!(T, U, V) fn, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
thenCompose
CompletableFuture!(U) thenCompose(FunctionT!(CompletionStage!(U)) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
thenComposeAsync
CompletableFuture!(U) thenComposeAsync(FunctionT!(CompletionStage!(U)) fn)
Undocumented in source. Be warned that the author may not have intended to support it.
thenComposeAsync
CompletableFuture!(U) thenComposeAsync(FunctionT!(CompletionStage!(U)) fn, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
thenRun
CompletableFuture!(void) thenRun(Runnable action)
Undocumented in source. Be warned that the author may not have intended to support it.
thenRunAsync
CompletableFuture!(void) thenRunAsync(Runnable action)
Undocumented in source. Be warned that the author may not have intended to support it.
thenRunAsync
CompletableFuture!(void) thenRunAsync(Runnable action, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.
toCompletableFuture
CompletableFuture!(T) toCompletableFuture()

Returns this CompletableFuture.

toString
string toString()

Returns a string identifying this CompletableFuture, as well as its completion state. The state, in brackets, contains the String {@code "Completed Normally"} or the String {@code "Completed Exceptionally"}, or the String {@code "Not completed"} followed by the number of CompletableFutures dependent upon its completion, if any.

tryPushStack
bool tryPushStack(Completion c)

Returns true if successfully pushed c onto stack.

uniExceptionally
bool uniExceptionally(CompletableFuture!(T) r, Function!(Throwable, T) f, UniExceptionally!(T) c)
Undocumented in source. Be warned that the author may not have intended to support it.
uniHandle
bool uniHandle(CompletableFuture!(S) r, BiFunction!(S, Throwable, T) f, UniHandle!(S, T) c)
Undocumented in source. Be warned that the author may not have intended to support it.
uniWhenComplete
bool uniWhenComplete(CompletableFuture!(T) r, BiConsumerT f, UniWhenComplete!(T) c)
Undocumented in source. Be warned that the author may not have intended to support it.
unipush
void unipush(Completion c)

Pushes the given completion unless it completes while trying. Caller should first check that result is null.

whenComplete
CompletableFuture!(T) whenComplete(BiConsumerT action)
Undocumented in source. Be warned that the author may not have intended to support it.
whenCompleteAsync
CompletableFuture!(T) whenCompleteAsync(BiConsumerT action)
Undocumented in source. Be warned that the author may not have intended to support it.
whenCompleteAsync
CompletableFuture!(T) whenCompleteAsync(BiConsumerT action, Executor executor)
Undocumented in source. Be warned that the author may not have intended to support it.

Inherited Members

From AbstractCompletableFuture

_isDone
bool _isDone;
Undocumented in source.
_isNull
bool _isNull;
Undocumented in source.
altResult
AltResult altResult;
Undocumented in source.
stack
Completion stack;
Undocumented in source.
bipush
void bipush(AbstractCompletableFuture b, BiCompletion c)
Undocumented in source.
cleanStack
void cleanStack()
Undocumented in source.
completeExceptionally
bool completeExceptionally(Throwable ex)
Undocumented in source.
postComplete
void postComplete()
Undocumented in source.
unipush
void unipush(Completion c)
Undocumented in source.
isDone
bool isDone()

Returns {@code true} if completed in any fashion: normally, exceptionally, or via cancellation.

isCancelled
bool isCancelled()

Returns {@code true} if this CompletableFuture was cancelled before it completed normally.

isCompletedExceptionally
bool isCompletedExceptionally()

Returns {@code true} if this CompletableFuture completed exceptionally, in any way. Possible causes include cancellation, explicit invocation of {@code completeExceptionally}, and abrupt termination of a CompletionStage action.

isCompletedSuccessfully
bool isCompletedSuccessfully()
Undocumented in source. Be warned that the author may not have intended to support it.
isFaulted
alias isFaulted = isCompletedExceptionally
Undocumented in source.
isCompleted
alias isCompleted = isDone
Undocumented in source.

Examples

{@code supplyAsync(supplier, delayedExecutor(timeout, timeUnit))}. To support methods with delays and timeouts, this class maintains at most one daemon thread for triggering and cancelling actions, not for running them.

<li>All CompletionStage methods are implemented independently of other methods, so the behavior of one method is not impacted by overrides of others in subclasses.

<li>All CompletionStage methods return CompletableFutures. To restrict usages to only those methods defined in interface CompletionStage, use method {@link #minimalCompletionStage}. Or to ensure only that clients do not themselves modify a future, use method {@link #copy}. </ul>

<p>CompletableFuture also implements {@link Future} with the following policies: <ul>

<li>Since (unlike {@link FutureTask}) this class has no direct control over the computation that causes it to be completed, cancellation is treated as just another form of exceptional completion. Method {@link #cancel cancel} has the same effect as {@code completeExceptionally(new CancellationException())}. Method {@link #isCompletedExceptionally} can be used to determine if a CompletableFuture completed in any exceptional fashion.

<li>In case of exceptional completion with a CompletionException, methods {@link #get()} and {@link #get(long, TimeUnit)} throw an {@link ExecutionException} with the same cause as held in the corresponding CompletionException. To simplify usage in most contexts, this class also defines methods {@link #join()} and {@link #getNow} that instead throw the CompletionException directly in these cases. </ul>

<p>Arguments used to pass a completion result (that is, for parameters of type {@code T}) for methods accepting them may be null, but passing a null value for any other parameter will result in a {@link NullPointerException} being thrown.

<p>Subclasses of this class should normally override the "virtual constructor" method {@link #newIncompleteFuture}, which establishes the concrete type returned by CompletionStage methods. For example, here is a class that substitutes a different default Executor and disables the {@code obtrude} methods:

<pre> {@code class MyCompletableFuture(T) : CompletableFuture!(T) { final Executor myExecutor = ...; MyCompletableFuture() { } <U> CompletableFuture!(U) newIncompleteFuture() { return new MyCompletableFuture!(U)(); } Executor defaultExecutor() { return myExecutor; } void obtrudeValue(T value) { throw new UnsupportedOperationException(); } void obtrudeException(Throwable ex) { throw new UnsupportedOperationException(); } }}</pre>

@author Doug Lea @param <T> The result type returned by this future's {@code join} and {@code get} methods

Meta