join
and get methods
Future that may be explicitly completed (setting its
value and status), and may be used as a CompletionStage,
supporting dependent functions and actions that trigger upon its
completion.
When two or more threads attempt to
complete,
completeExceptionally, or
cancel
a CompletableFuture, only one of them succeeds.
In addition to these and related methods for directly
manipulating status and results, CompletableFuture implements
interface CompletionStage with the following policies:
ForkJoinPool#commonPool(). The default async Executor may
be overridden for non-static methods in subclasses by defining
method defaultExecutor(). To simplify monitoring,
debugging, and tracking, all generated asynchronous tasks are
instances of the marker interface AsynchronousCompletionTask. Operations with time-delays can use
adapter methods defined in this class, for example: supplyAsync(supplier, delayedExecutor(timeout, timeUnit)).
minimalCompletionStage. Or to
ensure only that clients do not themselves modify a future, use
method copy.
CompletableFuture also implements Future with the following
policies:
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 cancel has the same effect as
completeExceptionally(new CancellationException()). Method
isCompletedExceptionally can be used to determine if a
CompletableFuture completed in any exceptional fashion.
get() and get(long, TimeUnit) throw an
ExecutionException with the same cause as held in the
corresponding CompletionException. To simplify usage in most
contexts, this class also defines methods join() and
getNow that instead throw the CompletionException directly
in these cases.
Arguments used to pass a completion result (that is, for
parameters of type T) for methods accepting them may be
null, but passing a null value for any other parameter will result
in a NullPointerException being thrown.
Subclasses of this class should normally override the "virtual
constructor" method 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 obtrude methods:
class MyCompletableFuture<T> extends CompletableFuture<T> {
static final Executor myExecutor = ...;
public MyCompletableFuture() { }
public <U> CompletableFuture<U> newIncompleteFuture() {
return new MyCompletableFuture<U>(); }
public Executor defaultExecutor() {
return myExecutor; }
public void obtrudeValue(T value) {
throw new UnsupportedOperationException(); }
public void obtrudeException(Throwable ex) {
throw new UnsupportedOperationException(); }
}
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static class | CompletableFuture.
Completion for an anyOf input future. |
| public static interface | CompletableFuture.
A marker interface identifying asynchronous tasks produced by
|
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv abstract static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | CompletableFuture.
Action to cancel unneeded timeouts |
| pack-priv static class | CompletableFuture.
A Completion delegating to a BiCompletion |
| pack-priv abstract static class | |
| pack-priv static class | |
| pack-priv static class | CompletableFuture.
A subclass that just throws UOE for most non-CompletionStage methods. |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | CompletableFuture.
Completion for recording and releasing a waiting thread. |
| pack-priv static class | CompletableFuture.
Action to submit user task |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv abstract static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final int | |
| private static final ForkJoinPool | ASYNC_POOL
Default Executor |
| pack-priv static final int | |
| private static final VarHandle | |
| pack-priv static final CompletableFuture. | NIL
The encoding of the null value. |
| pack-priv volatile Object | |
| private static final VarHandle | |
| pack-priv volatile CompletableFuture. | |
| private static final VarHandle | |
| pack-priv static final int |
| Access | Constructor and Description |
|---|---|
| public | |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public CompletableFuture | acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
Implements java. Returns a new CompletionStage that, when either |
| public CompletableFuture | acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
Implements java. Returns a new CompletionStage that, when either |
| public CompletableFuture | acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
Implements java. Returns a new CompletionStage that, when either |
| public static CompletableFuture | Returns: a new CompletableFuture that is completed when all of the given CompletableFutures completethe CompletableFutures cfs)Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. |
| pack-priv static CompletableFuture | |
| public static CompletableFuture | Returns: a new CompletableFuture that is completed with the result or exception of any of the given CompletableFutures when one completesthe CompletableFutures cfs)Returns a new CompletableFuture that is completed when any of the given CompletableFutures complete, with the same result. |
| public <U> CompletableFuture | applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)
Implements java. Returns a new CompletionStage that, when either |
| public <U> CompletableFuture | applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn)
Implements java. Returns a new CompletionStage that, when either |
| public <U> CompletableFuture | applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor)
Implements java. Returns a new CompletionStage that, when either |
| private <U> void | arrangeTimeout(long nanoDelay, CompletableFuture.
Schedules a timeout action, as well as whenComplete handling to cancel the action if not needed. |
| pack-priv static CompletableFuture | |
| pack-priv static <U> CompletableFuture | |
| pack-priv final <R, S> boolean | biAccept(Object r, Object s, BiConsumer<? super R, ? super S> f, CompletableFuture.
|
| private <U> CompletableFuture | |
| pack-priv final <R, S> boolean | biApply(Object r, Object s, BiFunction<? super R, ? super S, ? extends T> f, CompletableFuture.
|
| private <U, V> CompletableFuture | |
| pack-priv final void | bipush(CompletableFuture<?> b, CompletableFuture.
Pushes completion to this and b unless both done. |
| pack-priv final boolean | |
| private CompletableFuture | |
| public boolean | Returns: true if this task is now cancelledthis value has no effect in this
implementation because interrupts are not used to control
processing. mayInterruptIfRunning)Implements java. If not already completed, completes this CompletableFuture with
a |
| pack-priv final void | |
| public boolean | |
| public CompletableFuture | Returns: this CompletableFuturea function returning the value to be used
to complete this CompletableFuture supplier, Executor the executor to use for asynchronous execution executor)Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the given executor. |
| public CompletableFuture | Returns: this CompletableFuturea function returning the value to be used
to complete this CompletableFuture supplier)Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the default executor. |
| public static < the type of the value U> CompletableFuture | Returns: the completed CompletableFuturethe value value)Returns a new CompletableFuture that is already completed with the given value. |
| public static < the type of the value U> CompletionStage | Returns: the completed CompletionStagethe value value)Returns a new CompletionStage that is already completed with
the given value and supports only those methods in
interface |
| public boolean | Returns: true if this invocation caused this CompletableFuture
to transition to a completed state, else falsethe exception ex)If not already completed, causes invocations of |
| pack-priv final boolean | |
| public CompletableFuture | Returns: this CompletableFuturethe value to use upon timeout value, long how long to wait before completing normally
with the given value, in units of timeout, TimeUnit unita unit)TimeUnit determining how to interpret the
timeout parameterCompletes this CompletableFuture with the given value if not otherwise completed before the given timeout elapsed. |
| pack-priv final boolean | |
| pack-priv final boolean | |
| pack-priv final boolean | completeThrowable(Throwable x, Object r)
Completes with the given (non-null) exceptional result as a wrapped CompletionException unless it is one already, unless already completed. |
| pack-priv final boolean | |
| public CompletableFuture | Returns: the new CompletableFutureReturns a new CompletableFuture that is completed normally with the same value as this CompletableFuture when it completes normally. |
| public Executor | Returns: the executorReturns the default Executor used for async methods that do not specify an Executor. |
| public static Executor | Returns: the new delayed executorhow long to delay, in units of delay, TimeUnit unita unit, Executor TimeUnit determining how to interpret the
delay parameterthe base executor executor)Returns a new Executor that submits a task to the given base executor after the given delay (or no delay if non-positive). |
| public static Executor | Returns: the new delayed executorhow long to delay, in units of delay, TimeUnit unita unit)TimeUnit determining how to interpret the
delay parameterReturns a new Executor that submits a task to the default executor after the given delay (or no delay if non-positive). |
| pack-priv Object | encodeOutcome(T t, Throwable x)
Returns the encoding of the given arguments: if the exception is non-null, encodes as AltResult. |
| pack-priv static Object | encodeRelay(Object r)
Returns the encoding of a copied outcome; if exceptional, rewraps as a CompletionException, else returns argument. |
| pack-priv static CompletableFuture. | encodeThrowable(Throwable x)
Returns the encoding of the given (non-null) exception as a wrapped CompletionException unless it is one already. |
| pack-priv static Object | encodeThrowable(Throwable x, Object r)
Returns the encoding of the given (non-null) exception as a wrapped CompletionException unless it is one already. |
| pack-priv final Object | |
| public CompletableFuture | exceptionally(Function<Throwable, ? extends T> fn)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | exceptionallyAsync(Function<Throwable, ? extends T> fn)
Overrides default java. Returns a new CompletionStage that, when |
| public CompletableFuture | exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor executor)
Overrides default java. Returns a new CompletionStage that, when |
| public CompletableFuture | exceptionallyCompose(Function<Throwable, ? extends CompletionStage<T>> fn)
Overrides default java. Returns a new CompletionStage that, when |
| public CompletableFuture | exceptionallyComposeAsync(Function<Throwable, ? extends CompletionStage<T>> fn)
Overrides default java. Returns a new CompletionStage that, when |
| public CompletableFuture | exceptionallyComposeAsync(Function<Throwable, ? extends CompletionStage<T>> fn, Executor executor)
Overrides default java. Returns a new CompletionStage that, when |
| public Throwable | exceptionNow()
Overrides default java. Returns the exception thrown by the task, without waiting. |
| public static < the type of the value U> CompletableFuture | Returns: the exceptionally completed CompletableFuturethe exception ex)Returns a new CompletableFuture that is already completed exceptionally with the given exception. |
| public static < the type of the value U> CompletionStage | Returns: the exceptionally completed CompletionStagethe exception ex)Returns a new CompletionStage that is already completed
exceptionally with the given exception and supports only those
methods in interface |
| public T | Returns: the result valueImplements java. Waits if necessary for this future to complete, and then returns its result. |
| public T | Returns: the result valuethe 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 this future to complete, and then returns its result, if available. |
| public T | Returns: the result value, if completed, else the given valueIfAbsentthe value to return if not completed valueIfAbsent)Returns the result value (or throws any encountered exception) if completed, else returns the given valueIfAbsent. |
| public int | Returns: the number of dependent CompletableFuturesReturns the estimated number of CompletableFutures whose completions are awaiting completion of this CompletableFuture. |
| public <U> CompletableFuture | handle(BiFunction<? super T, Throwable, ? extends U> fn)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor)
Implements java. Returns a new CompletionStage that, when |
| pack-priv final boolean | |
| public boolean | Returns: true if this CompletableFuture was cancelled
before it completed normallyImplements java. Returns |
| public boolean | Returns: true if this CompletableFuture completed
exceptionallyReturns |
| public boolean | Returns: true if completedImplements java. Returns |
| public T | Returns: the result valueReturns the result value when complete, or throws an (unchecked) exception if completed exceptionally. |
| public CompletionStage | Returns: the new CompletionStageReturns 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 |
| public < the type of the value U> CompletableFuture | Returns: a new CompletableFutureReturns a new incomplete CompletableFuture of the type to be returned by a CompletionStage method. |
| public void | obtrudeException(Throwable
the exception ex)Forcibly causes subsequent invocations of method |
| public void | obtrudeValue(T
the completion value value)Forcibly sets or resets the value subsequently returned by
method |
| private <U extends T> CompletableFuture | |
| private <U extends T, V> CompletableFuture | |
| pack-priv final void | orpush(CompletableFuture<?> b, CompletableFuture.
Pushes completion to this and b unless either done. |
| private CompletableFuture | |
| public CompletableFuture | Returns: this CompletableFuturehow long to wait before completing exceptionally
with a TimeoutException, in units of timeout, TimeUnit unita unit)TimeUnit determining how to interpret the
timeout parameterExceptionally completes this CompletableFuture with
a |
| pack-priv final void | |
| pack-priv final CompletableFuture | postFire(CompletableFuture<?> a, int mode)
Post-processing by dependent after successful UniCompletion tryFire. |
| pack-priv final CompletableFuture | postFire(CompletableFuture<?> a, CompletableFuture<?> b, int mode)
Post-processing after successful BiCompletion tryFire. |
| pack-priv final void | pushStack(CompletableFuture.
Unconditionally pushes c onto stack, retrying if necessary. |
| private static Object | |
| private static Object | |
| public T | resultNow()
Overrides default java. Returns the computed result, without waiting. |
| public CompletableFuture | runAfterBoth(CompletionStage<?> other, Runnable action)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | runAfterBothAsync(CompletionStage<?> other, Runnable action)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | runAfterEither(CompletionStage<?> other, Runnable action)
Implements java. Returns a new CompletionStage that, when either |
| public CompletableFuture | runAfterEitherAsync(CompletionStage<?> other, Runnable action)
Implements java. Returns a new CompletionStage that, when either |
| public CompletableFuture | runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor)
Implements java. Returns a new CompletionStage that, when either |
| public static CompletableFuture | Returns: the new CompletableFuturethe action to run before completing the
returned CompletableFuture runnable)Returns a new CompletableFuture that is asynchronously completed
by a task running in the |
| public static CompletableFuture | Returns: the new CompletableFuturethe action to run before completing the
returned CompletableFuture runnable, Executor the executor to use for asynchronous execution executor)Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor after it runs the given action. |
| public Future. | |
| public static < the function's return type U> CompletableFuture | Returns: the new CompletableFuturea function returning the value to be used
to complete the returned CompletableFuture supplier)Returns a new CompletableFuture that is asynchronously completed
by a task running in the |
| public static < the function's return type U> CompletableFuture | Returns: the new CompletableFuturea function returning the value to be used
to complete the returned CompletableFuture supplier, Executor the executor to use for asynchronous execution executor)Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor with the value obtained by calling the given Supplier. |
| public CompletableFuture | thenAccept(Consumer<? super T> action)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | thenAcceptAsync(Consumer<? super T> action)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | thenAcceptAsync(Consumer<? super T> action, Executor executor)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | thenApply(Function<? super T, ? extends U> fn)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | thenApplyAsync(Function<? super T, ? extends U> fn)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor)
Implements java. Returns a new CompletionStage that, when |
| public <U, V> CompletableFuture | thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)
Implements java. Returns a new CompletionStage that, when |
| public <U, V> CompletableFuture | thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)
Implements java. Returns a new CompletionStage that, when |
| public <U, V> CompletableFuture | thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor)
Implements java. Returns a new CompletionStage that, when |
| public <U> CompletableFuture | thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)
Implements java. Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function. |
| public <U> CompletableFuture | thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn)
Implements java. Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function,
executed using |
| public <U> CompletableFuture | thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor)
Implements java. Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function, executed using the supplied Executor. |
| public CompletableFuture | thenRun(Runnable action)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | thenRunAsync(Runnable action)
Implements java. Returns a new CompletionStage that, when |
| public CompletableFuture | thenRunAsync(Runnable action, Executor executor)
Implements java. Returns a new CompletionStage that, when |
| private Object | timedGet(long nanos)
Returns raw result after waiting, or null if interrupted, or throws TimeoutException on timeout. |
| public CompletableFuture | Returns: this CompletableFutureImplements java. Returns this CompletableFuture. |
| public String | Returns: a string identifying this CompletableFuture, as well as its stateOverrides java. Returns a string identifying this CompletableFuture, as well as its completion state. |
| pack-priv final boolean | |
| private CompletableFuture | |
| private CompletableFuture | |
| private <V> CompletableFuture | |
| private <V> CompletableFuture | |
| private CompletableFuture. | |
| private CompletableFuture | |
| private <V> CompletableFuture | |
| private static <U, T extends U> CompletableFuture | |
| pack-priv final boolean | uniExceptionally(Object r, Function<? super Throwable, ? extends T> f, CompletableFuture.
|
| private CompletableFuture | |
| pack-priv final <S> boolean | uniHandle(Object r, BiFunction<? super S, Throwable, ? extends T> f, CompletableFuture.
|
| private <V> CompletableFuture | |
| pack-priv final void | unipush(CompletableFuture.
Pushes the given completion unless it completes while trying. |
| private CompletableFuture | |
| private CompletableFuture | |
| pack-priv final boolean | uniWhenComplete(Object r, BiConsumer<? super T, ? super Throwable> f, CompletableFuture.
|
| private CompletableFuture | |
| private Object | waitingGet(boolean interruptible)
Returns raw result after waiting, or null if interruptible and interrupted. |
| public CompletableFuture | whenComplete(BiConsumer<? super T, ? super Throwable> action)
Implements java. Returns a new CompletionStage with the same result or exception as
|
| public CompletableFuture | whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)
Implements java. Returns a new CompletionStage with the same result or exception as
|
| public CompletableFuture | whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)
Implements java. Returns a new CompletionStage with the same result or exception as
|
| pack-priv static CompletionException | |
| pack-priv static ExecutionException |