public class CompletableFuture<T> extends Object implements Future<T>, CompletionStep<T>
When two or more threads attempt to { #complete complete}, { #completeExceptionally completeExceptionally}, or { #cancel cancel} a CompletableFuture, only one of them succeeds.
In addition to these and related methods for directly manipulating status and results, CompletableFuture implements interface { CompletionStep} with the following policies:
CompletableFuture also implements { Future} with the following policies:
completeExceptionally(new CancellationException()). Method
{ #isCompletedExceptionally} can be used to determine if a
CompletableFuture completed in any exceptional fashion.
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
CompletableFuture.AsynchronousCompletionTask
A marker interface identifying asynchronous tasks produced by
async methods. |
| 构造器和说明 |
|---|
CompletableFuture()
Creates a new incomplete CompletableFuture.
|
| 限定符和类型 | 方法和说明 |
|---|---|
CompletableFuture<Void> |
acceptEither(CompletionStep<? extends T> other,
Consumer<? super T> action)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, is executed with the
corresponding result as argument to the supplied action.
|
CompletableFuture<Void> |
acceptEitherAsync(CompletionStep<? extends T> other,
Consumer<? super T> action)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, is executed using this
stage's default asynchronous execution facility, with the
corresponding result as argument to the supplied action.
|
CompletableFuture<Void> |
acceptEitherAsync(CompletionStep<? extends T> other,
Consumer<? super T> action,
Executor executor)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, is executed using the
supplied executor, with the corresponding result as argument to
the supplied action.
|
static CompletableFuture<Void> |
allOf(CompletableFuture<?>... cfs)
Returns a new CompletableFuture that is completed when all of
the given CompletableFutures complete.
|
static CompletableFuture<Object> |
anyOf(CompletableFuture<?>... cfs)
Returns a new CompletableFuture that is completed when any of
the given CompletableFutures complete, with the same result.
|
<U> CompletableFuture<U> |
applyToEither(CompletionStep<? extends T> other,
Function<? super T,U> fn)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, is executed with the
corresponding result as argument to the supplied function.
|
<U> CompletableFuture<U> |
applyToEitherAsync(CompletionStep<? extends T> other,
Function<? super T,U> fn)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, is executed using this
stage's default asynchronous execution facility, with the
corresponding result as argument to the supplied function.
|
<U> CompletableFuture<U> |
applyToEitherAsync(CompletionStep<? extends T> other,
Function<? super T,U> fn,
Executor executor)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, is executed using the
supplied executor, with the corresponding result as argument to
the supplied function.
|
boolean |
cancel(boolean mayInterruptIfRunning)
If not already completed, completes this CompletableFuture with
a { CancellationException}.
|
boolean |
complete(T value)
If not already completed, sets the value returned by {
#get()} and related methods to the given value.
|
static <U> CompletableFuture<U> |
completedFuture(U value)
Returns a new CompletableFuture that is already completed with
the given value.
|
boolean |
completeExceptionally(Throwable ex)
If not already completed, causes invocations of { #get()}
and related methods to throw the given exception.
|
CompletableFuture<T> |
exceptionally(Function<Throwable,? extends 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.
|
T |
get()
Waits if necessary for this future to complete, and then
returns its result.
|
T |
get(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for this future
to complete, and then returns its result, if available.
|
T |
getNow(T valueIfAbsent)
Returns the result value (or throws any encountered exception)
if completed, else returns the given valueIfAbsent.
|
int |
getNumberOfDependents()
Returns the estimated number of CompletableFutures whose
completions are awaiting completion of this CompletableFuture.
|
<U> CompletableFuture<U> |
handle(Function2<? super T,Throwable,? extends U> fn)
Returns a new CompletionStage that, when this stage completes
either normally or exceptionally, is executed with this stage's
result and exception as arguments to the supplied function.
|
<U> CompletableFuture<U> |
handleAsync(Function2<? super T,Throwable,? extends U> fn)
Returns a new CompletionStage that, when this stage completes
either normally or exceptionally, is executed using this stage's
default asynchronous execution facility, with this stage's
result and exception as arguments to the supplied function.
|
<U> CompletableFuture<U> |
handleAsync(Function2<? super T,Throwable,? extends U> fn,
Executor executor)
Returns a new CompletionStage that, when this stage completes
either normally or exceptionally, is executed using the
supplied executor, with this stage's result and exception as
arguments to the supplied function.
|
boolean |
isCancelled()
Returns
true if this CompletableFuture was cancelled
before it completed normally. |
boolean |
isCompletedExceptionally()
Returns
true if this CompletableFuture completed
exceptionally, in any way. |
boolean |
isDone()
Returns
true if completed in any fashion: normally,
exceptionally, or via cancellation. |
T |
join()
Returns the result value when complete, or throws an
(unchecked) exception if completed exceptionally.
|
void |
obtrudeException(Throwable ex)
Forcibly causes subsequent invocations of method { #get()}
and related methods to throw the given exception, whether or
not already completed.
|
void |
obtrudeValue(T value)
Forcibly sets or resets the value subsequently returned by
method { #get()} and related methods, whether or not
already completed.
|
CompletableFuture<Void> |
runAfterBoth(CompletionStep<?> other,
Runnable action)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, executes the given action.
|
CompletableFuture<Void> |
runAfterBothAsync(CompletionStep<?> other,
Runnable action)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, executes the given action
using this stage's default asynchronous execution facility.
|
CompletableFuture<Void> |
runAfterBothAsync(CompletionStep<?> other,
Runnable action,
Executor executor)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, executes the given action
using the supplied executor.
|
CompletableFuture<Void> |
runAfterEither(CompletionStep<?> other,
Runnable action)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, executes the given action.
|
CompletableFuture<Void> |
runAfterEitherAsync(CompletionStep<?> other,
Runnable action)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, executes the given action
using this stage's default asynchronous execution facility.
|
CompletableFuture<Void> |
runAfterEitherAsync(CompletionStep<?> other,
Runnable action,
Executor executor)
Returns a new CompletionStage that, when either this or the
other given stage complete normally, executes the given action
using the supplied executor.
|
static CompletableFuture<Void> |
runAsync(Runnable runnable)
Returns a new CompletableFuture that is asynchronously completed
by a task running in the { ForkJoinPool#commonPool()} after
it runs the given action.
|
static CompletableFuture<Void> |
runAsync(Runnable runnable,
Executor executor)
Returns a new CompletableFuture that is asynchronously completed
by a task running in the given executor after it runs the given
action.
|
static <U> CompletableFuture<U> |
supplyAsync(Supplier0<U> supplier0)
Returns a new CompletableFuture that is asynchronously completed
by a task running in the { ForkJoinPool#commonPool()} with
the value obtained by calling the given Supplier0.
|
static <U> CompletableFuture<U> |
supplyAsync(Supplier0<U> supplier0,
Executor 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 Supplier0.
|
CompletableFuture<Void> |
thenAccept(Consumer<? super T> action)
Returns a new CompletionStage that, when this stage completes
normally, is executed with this stage's result as the argument
to the supplied action.
|
CompletableFuture<Void> |
thenAcceptAsync(Consumer<? super T> action)
Returns a new CompletionStage that, when this stage completes
normally, is executed using this stage's default asynchronous
execution facility, with this stage's result as the argument to
the supplied action.
|
CompletableFuture<Void> |
thenAcceptAsync(Consumer<? super T> action,
Executor executor)
Returns a new CompletionStage that, when this stage completes
normally, is executed using the supplied Executor, with this
stage's result as the argument to the supplied action.
|
<U> CompletableFuture<Void> |
thenAcceptBoth(CompletionStep<? extends U> other,
Consumer2<? super T,? super U> action)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, is executed with the two
results as arguments to the supplied action.
|
<U> CompletableFuture<Void> |
thenAcceptBothAsync(CompletionStep<? extends U> other,
Consumer2<? super T,? super U> action)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, is executed using this
stage's default asynchronous execution facility, with the two
results as arguments to the supplied action.
|
<U> CompletableFuture<Void> |
thenAcceptBothAsync(CompletionStep<? extends U> other,
Consumer2<? super T,? super U> action,
Executor executor)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, is executed using the
supplied executor, with the two results as arguments to the
supplied action.
|
<U> CompletableFuture<U> |
thenApply(Function<? super T,? extends U> fn)
Returns a new CompletionStage that, when this stage completes
normally, is executed with this stage's result as the argument
to the supplied function.
|
<U> CompletableFuture<U> |
thenApplyAsync(Function<? super T,? extends U> fn)
Returns a new CompletionStage that, when this stage completes
normally, is executed using this stage's default asynchronous
execution facility, with this stage's result as the argument to
the supplied function.
|
<U> CompletableFuture<U> |
thenApplyAsync(Function<? super T,? extends U> fn,
Executor executor)
Returns a new CompletionStage that, when this stage completes
normally, is executed using the supplied Executor, with this
stage's result as the argument to the supplied function.
|
<U,V> CompletableFuture<V> |
thenCombine(CompletionStep<? extends U> other,
Function2<? super T,? super U,? extends V> fn)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, is executed with the two
results as arguments to the supplied function.
|
<U,V> CompletableFuture<V> |
thenCombineAsync(CompletionStep<? extends U> other,
Function2<? super T,? super U,? extends V> fn)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, is executed using this
stage's default asynchronous execution facility, with the two
results as arguments to the supplied function.
|
<U,V> CompletableFuture<V> |
thenCombineAsync(CompletionStep<? extends U> other,
Function2<? super T,? super U,? extends V> fn,
Executor executor)
Returns a new CompletionStage that, when this and the other
given stage both complete normally, is executed using the
supplied executor, with the two results as arguments to the
supplied function.
|
<U> CompletableFuture<U> |
thenCompose(Function<? super T,? extends CompletionStep<U>> fn)
Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function.
|
<U> CompletableFuture<U> |
thenComposeAsync(Function<? super T,? extends CompletionStep<U>> fn)
Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function,
executed using this stage's default asynchronous execution
facility.
|
<U> CompletableFuture<U> |
thenComposeAsync(Function<? super T,? extends CompletionStep<U>> fn,
Executor executor)
Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function,
executed using the supplied Executor.
|
CompletableFuture<Void> |
thenRun(Runnable action)
Returns a new CompletionStage that, when this stage completes
normally, executes the given action.
|
CompletableFuture<Void> |
thenRunAsync(Runnable action)
Returns a new CompletionStage that, when this stage completes
normally, executes the given action using this stage's default
asynchronous execution facility.
|
CompletableFuture<Void> |
thenRunAsync(Runnable action,
Executor executor)
Returns a new CompletionStage that, when this stage completes
normally, executes the given action using the supplied Executor.
|
CompletableFuture<T> |
toCompletableFuture()
Returns this CompletableFuture.
|
String |
toString()
Returns a string identifying this CompletableFuture, as well as
its completion state.
|
CompletableFuture<T> |
whenComplete(Consumer2<? super T,? super Throwable> action)
Returns a new CompletionStage with the same result or exception as
this stage, that executes the given action when this stage completes.
|
CompletableFuture<T> |
whenCompleteAsync(Consumer2<? super T,? super Throwable> action)
Returns a new CompletionStage with the same result or exception as
this stage, that executes the given action using this stage's
default asynchronous execution facility when this stage completes.
|
CompletableFuture<T> |
whenCompleteAsync(Consumer2<? super T,? super Throwable> action,
Executor executor)
Returns a new CompletionStage with the same result or exception as
this stage, that executes the given action using the supplied
Executor when this stage completes.
|
public static <U> CompletableFuture<U> supplyAsync(Supplier0<U> supplier0)
U - the function's return typesupplier0 - a function returning the value to be used
to complete the returned CompletableFuturepublic static <U> CompletableFuture<U> supplyAsync(Supplier0<U> supplier0, Executor executor)
U - the function's return typesupplier0 - a function returning the value to be used
to complete the returned CompletableFutureexecutor - the executor to use for asynchronous executionpublic static CompletableFuture<Void> runAsync(Runnable runnable)
runnable - the action to run before completing the
returned CompletableFuturepublic static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
runnable - the action to run before completing the
returned CompletableFutureexecutor - the executor to use for asynchronous executionpublic static <U> CompletableFuture<U> completedFuture(U value)
U - the type of the valuevalue - the valuepublic boolean isDone()
true if completed in any fashion: normally,
exceptionally, or via cancellation.public T get() throws InterruptedException, ExecutionException
get 在接口中 Future<T>CancellationException - if this future was cancelledExecutionException - if this future completed exceptionallyInterruptedException - if the current thread was interrupted
while waitingpublic T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get 在接口中 Future<T>timeout - the maximum time to waitunit - the time unit of the timeout argumentCancellationException - if this future was cancelledExecutionException - if this future completed exceptionallyInterruptedException - if the current thread was interrupted
while waitingTimeoutException - if the wait timed outpublic T join()
CancellationException - if the computation was cancelledCompletionException - if this future completed
exceptionally or a completion computation threw an exceptionpublic T getNow(T valueIfAbsent)
valueIfAbsent - the value to return if not completedCancellationException - if the computation was cancelledCompletionException - if this future completed
exceptionally or a completion computation threw an exceptionpublic boolean complete(T value)
value - the result valuetrue if this invocation caused this CompletableFuture
to transition to a completed state, else falsepublic boolean completeExceptionally(Throwable ex)
ex - the exceptiontrue if this invocation caused this CompletableFuture
to transition to a completed state, else falsepublic <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
CompletionStep
This method is analogous to { java.util.Optional#map Optional.map} and { java.util.stream.Stream#map Stream.map}.
See the { CompletionStep} documentation for rules covering exceptional completion.
thenApply 在接口中 CompletionStep<T>U - the function's return typefn - the function to use to compute the value of the
returned CompletionStagepublic <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenApplyAsync 在接口中 CompletionStep<T>U - the function's return typefn - the function to use to compute the value of the
returned CompletionStagepublic <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenApplyAsync 在接口中 CompletionStep<T>U - the function's return typefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> thenAccept(Consumer<? super T> action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenAccept 在接口中 CompletionStep<T>action - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenAcceptAsync 在接口中 CompletionStep<T>action - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenAcceptAsync 在接口中 CompletionStep<T>action - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> thenRun(Runnable action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenRun 在接口中 CompletionStep<T>action - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> thenRunAsync(Runnable action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenRunAsync 在接口中 CompletionStep<T>action - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenRunAsync 在接口中 CompletionStep<T>action - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic <U,V> CompletableFuture<V> thenCombine(CompletionStep<? extends U> other, Function2<? super T,? super U,? extends V> fn)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenCombine 在接口中 CompletionStep<T>U - the type of the other CompletionStage's resultV - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStagepublic <U,V> CompletableFuture<V> thenCombineAsync(CompletionStep<? extends U> other, Function2<? super T,? super U,? extends V> fn)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenCombineAsync 在接口中 CompletionStep<T>U - the type of the other CompletionStage's resultV - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStagepublic <U,V> CompletableFuture<V> thenCombineAsync(CompletionStep<? extends U> other, Function2<? super T,? super U,? extends V> fn, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenCombineAsync 在接口中 CompletionStep<T>U - the type of the other CompletionStage's resultV - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic <U> CompletableFuture<Void> thenAcceptBoth(CompletionStep<? extends U> other, Consumer2<? super T,? super U> action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenAcceptBoth 在接口中 CompletionStep<T>U - the type of the other CompletionStage's resultother - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStep<? extends U> other, Consumer2<? super T,? super U> action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenAcceptBothAsync 在接口中 CompletionStep<T>U - the type of the other CompletionStage's resultother - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStep<? extends U> other, Consumer2<? super T,? super U> action, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
thenAcceptBothAsync 在接口中 CompletionStep<T>U - the type of the other CompletionStage's resultother - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> runAfterBoth(CompletionStep<?> other, Runnable action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
runAfterBoth 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> runAfterBothAsync(CompletionStep<?> other, Runnable action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
runAfterBothAsync 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> runAfterBothAsync(CompletionStep<?> other, Runnable action, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
runAfterBothAsync 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic <U> CompletableFuture<U> applyToEither(CompletionStep<? extends T> other, Function<? super T,U> fn)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
applyToEither 在接口中 CompletionStep<T>U - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStagepublic <U> CompletableFuture<U> applyToEitherAsync(CompletionStep<? extends T> other, Function<? super T,U> fn)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
applyToEitherAsync 在接口中 CompletionStep<T>U - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStagepublic <U> CompletableFuture<U> applyToEitherAsync(CompletionStep<? extends T> other, Function<? super T,U> fn, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
applyToEitherAsync 在接口中 CompletionStep<T>U - the function's return typeother - the other CompletionStagefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> acceptEither(CompletionStep<? extends T> other, Consumer<? super T> action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
acceptEither 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> acceptEitherAsync(CompletionStep<? extends T> other, Consumer<? super T> action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
acceptEitherAsync 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> acceptEitherAsync(CompletionStep<? extends T> other, Consumer<? super T> action, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
acceptEitherAsync 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> runAfterEither(CompletionStep<?> other, Runnable action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
runAfterEither 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> runAfterEitherAsync(CompletionStep<?> other, Runnable action)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
runAfterEitherAsync 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStagepublic CompletableFuture<Void> runAfterEitherAsync(CompletionStep<?> other, Runnable action, Executor executor)
CompletionStepSee the { CompletionStep} documentation for rules covering exceptional completion.
runAfterEitherAsync 在接口中 CompletionStep<T>other - the other CompletionStageaction - the action to perform before completing the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic <U> CompletableFuture<U> thenCompose(Function<? super T,? extends CompletionStep<U>> fn)
CompletionStep
When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.
To ensure progress, the supplied function must arrange eventual completion of its result.
This method is analogous to { java.util.Optional#flatMap Optional.flatMap} and { java.util.stream.Stream#flatMap Stream.flatMap}.
See the { CompletionStep} documentation for rules covering exceptional completion.
thenCompose 在接口中 CompletionStep<T>U - the type of the returned CompletionStage's resultfn - the function to use to compute another CompletionStagepublic <U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStep<U>> fn)
CompletionStep
When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.
To ensure progress, the supplied function must arrange eventual completion of its result.
See the { CompletionStep} documentation for rules covering exceptional completion.
thenComposeAsync 在接口中 CompletionStep<T>U - the type of the returned CompletionStage's resultfn - the function to use to compute another CompletionStagepublic <U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStep<U>> fn, Executor executor)
CompletionStep
When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.
To ensure progress, the supplied function must arrange eventual completion of its result.
See the { CompletionStep} documentation for rules covering exceptional completion.
thenComposeAsync 在接口中 CompletionStep<T>U - the type of the returned CompletionStage's resultfn - the function to use to compute another CompletionStageexecutor - the executor to use for asynchronous executionpublic CompletableFuture<T> whenComplete(Consumer2<? super T,? super Throwable> action)
CompletionStep
When this stage is complete, the given action is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments. The returned
stage is completed when the action returns.
Unlike method { #handle handle}, this method is not designed to translate completion outcomes, so the supplied action should not throw an exception. However, if it does, the following rules apply: if this stage completed normally but the supplied action throws an exception, then the returned stage completes exceptionally with the supplied action's exception. Or, if this stage completed exceptionally and the supplied action throws an exception, then the returned stage completes exceptionally with this stage's exception.
whenComplete 在接口中 CompletionStep<T>action - the action to performpublic CompletableFuture<T> whenCompleteAsync(Consumer2<? super T,? super Throwable> action)
CompletionStep
When this stage is complete, the given action is invoked with the
result (or null if none) and the exception (or null
if none) of this stage as arguments. The returned stage is completed
when the action returns.
Unlike method { #handleAsync(Function2) handleAsync}, this method is not designed to translate completion outcomes, so the supplied action should not throw an exception. However, if it does, the following rules apply: If this stage completed normally but the supplied action throws an exception, then the returned stage completes exceptionally with the supplied action's exception. Or, if this stage completed exceptionally and the supplied action throws an exception, then the returned stage completes exceptionally with this stage's exception.
whenCompleteAsync 在接口中 CompletionStep<T>action - the action to performpublic CompletableFuture<T> whenCompleteAsync(Consumer2<? super T,? super Throwable> action, Executor executor)
CompletionStep
When this stage is complete, the given action is invoked with the
result (or null if none) and the exception (or null
if none) of this stage as arguments. The returned stage is completed
when the action returns.
Unlike method { #handleAsync(Function2,Executor) handleAsync}, this method is not designed to translate completion outcomes, so the supplied action should not throw an exception. However, if it does, the following rules apply: If this stage completed normally but the supplied action throws an exception, then the returned stage completes exceptionally with the supplied action's exception. Or, if this stage completed exceptionally and the supplied action throws an exception, then the returned stage completes exceptionally with this stage's exception.
whenCompleteAsync 在接口中 CompletionStep<T>action - the action to performexecutor - the executor to use for asynchronous executionpublic <U> CompletableFuture<U> handle(Function2<? super T,Throwable,? extends U> fn)
CompletionStep
When this stage is complete, the given function is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments, and the
function's result is used to complete the returned stage.
handle 在接口中 CompletionStep<T>U - the function's return typefn - the function to use to compute the value of the
returned CompletionStagepublic <U> CompletableFuture<U> handleAsync(Function2<? super T,Throwable,? extends U> fn)
CompletionStep
When this stage is complete, the given function is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments, and the
function's result is used to complete the returned stage.
handleAsync 在接口中 CompletionStep<T>U - the function's return typefn - the function to use to compute the value of the
returned CompletionStagepublic <U> CompletableFuture<U> handleAsync(Function2<? super T,Throwable,? extends U> fn, Executor executor)
CompletionStep
When this stage is complete, the given function is invoked
with the result (or null if none) and the exception (or
null if none) of this stage as arguments, and the
function's result is used to complete the returned stage.
handleAsync 在接口中 CompletionStep<T>U - the function's return typefn - the function to use to compute the value of the
returned CompletionStageexecutor - the executor to use for asynchronous executionpublic CompletableFuture<T> toCompletableFuture()
toCompletableFuture 在接口中 CompletionStep<T>public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)
whenComplete and handle.exceptionally 在接口中 CompletionStep<T>fn - the function to use to compute the value of the
returned CompletableFuture if this CompletableFuture completed
exceptionallypublic static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
null.
Among the applications of this method is to await completion
of a set of independent CompletableFutures before continuing a
program, as in: CompletableFuture.allOf(c1, c2,
c3).join();.
cfs - the CompletableFuturesNullPointerException - if the array or any of its elements are
nullpublic static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
cfs - the CompletableFuturesNullPointerException - if the array or any of its elements are
nullpublic boolean cancel(boolean mayInterruptIfRunning)
CancellationException.public boolean isCancelled()
true if this CompletableFuture was cancelled
before it completed normally.isCancelled 在接口中 Future<T>true if this CompletableFuture was cancelled
before it completed normallypublic boolean isCompletedExceptionally()
true if this CompletableFuture completed
exceptionally, in any way. Possible causes include
cancellation, explicit invocation of completeExceptionally, and abrupt termination of a
CompletionStep action.true if this CompletableFuture completed
exceptionallypublic void obtrudeValue(T value)
value - the completion valuepublic void obtrudeException(Throwable ex)
ex - the exceptionNullPointerException - if the exception is nullpublic int getNumberOfDependents()
public String toString()
"Completed Normally" or the String "Completed Exceptionally", or the String "Not
completed" followed by the number of CompletableFutures
dependent upon its completion, if any.Copyright © 2021. All rights reserved.