public class CompletableFuture<T> extends Object implements Future<T>
Future that may be explicitly completed (setting its value and
status), and may include dependent functions and actions that trigger upon
its completion. Methods are available for adding those based on Functions,
Blocks, and Runnables, depending on whether they require arguments and/or
produce results, as well as those triggered after either or both the current
and another CompletableFuture complete. Functions and actions supplied for
dependent completions (mainly using methods with prefix then) may be performed by the thread that completes the current
CompletableFuture, or by any other caller of these methods. There are no
guarantees about the order of processing completions unless constrained by
these methods.
When two or more threads attempt to complete(T) or completeExceptionally(java.lang.Throwable) a CompletableFuture, only one of them succeeds.
Upon exceptional completion, or when a completion entails computation of a
function or action, and it terminates abruptly with an (unchecked) exception
or error, then further completions act as completeExceptionally with
a CompletionException holding that exception as its cause. If a
CompletableFuture completes exceptionally, and is not followed by a
exceptionally(org.skfiy.util.v8.CompletableFuture.Fun<java.lang.Throwable, ? extends T>) or handle(org.skfiy.util.v8.CompletableFuture.BiFun<? super T, java.lang.Throwable, ? extends U>) completion, then all of its
dependents (and their dependents) also complete exceptionally with
CompletionExceptions holding the ultimate cause. In case of a
CompletionException, methods get() and get(long,
TimeUnit) throw an ExecutionException with the same cause as would
be held in the corresponding CompletionException. However, in these cases,
methods join() and getNow(T) throw the CompletionException,
which simplifies usage especially within other completion functions.
CompletableFutures themselves do not execute asynchronously. However, the
async methods provide commonly useful ways to commence asynchronous
processing, using either a given Executor or by default the ForkJoinPool.commonPool(), of a function
or action that will result in the completion of a new CompletableFuture. To
simplify monitoring, debugging, and tracking, all generated asynchronous
tasks are instances of the marker interface
CompletableFuture.AsynchronousCompletionTask.
jsr166e note: During transition, this class uses nested functional interfaces with different names but the same forms as those expected for JDK8.
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
CompletableFuture.Action<A>
Interface describing a void action of one argument
|
static interface |
CompletableFuture.AsynchronousCompletionTask
A marker interface identifying asynchronous tasks produced by
async methods. |
static interface |
CompletableFuture.BiAction<A,B>
Interface describing a void action of two arguments
|
static interface |
CompletableFuture.BiFun<A,B,T>
Interface describing a function of two arguments
|
static interface |
CompletableFuture.Fun<A,T>
Interface describing a function of one argument
|
static interface |
CompletableFuture.Generator<T>
Interface describing a function of no arguments
|
| 构造器和说明 |
|---|
CompletableFuture()
Creates a new incomplete CompletableFuture.
|
| 限定符和类型 | 方法和说明 |
|---|---|
CompletableFuture<Void> |
acceptEither(CompletableFuture<? extends T> other,
CompletableFuture.Action<? super T> block)
Creates and returns a CompletableFuture that is completed after
performing the given action with the result of either this or the other
given CompletableFuture's result, when either complete.
|
CompletableFuture<Void> |
acceptEitherAsync(CompletableFuture<? extends T> other,
CompletableFuture.Action<? super T> block)
Creates and returns a CompletableFuture that is completed asynchronously
using the
ForkJoinPool.commonPool(), performing the given action
with the result of either this or the other given CompletableFuture's
result, when either complete. |
CompletableFuture<Void> |
acceptEitherAsync(CompletableFuture<? extends T> other,
CompletableFuture.Action<? super T> block,
Executor executor)
Creates and returns a CompletableFuture that is completed asynchronously
using the given executor, performing the given action with the result of
either this or the other given CompletableFuture's result, when either
complete.
|
<U> CompletableFuture<U> |
applyToEither(CompletableFuture<? extends T> other,
CompletableFuture.Fun<? super T,U> fn)
Creates and returns a CompletableFuture that is completed with the result
of the given function of either this or the other given
CompletableFuture's results when either complete.
|
<U> CompletableFuture<U> |
applyToEitherAsync(CompletableFuture<? extends T> other,
CompletableFuture.Fun<? super T,U> fn)
Creates and returns a CompletableFuture that is completed asynchronously
using the
ForkJoinPool.commonPool() with the result of the given
function of either this or the other given CompletableFuture's results
when either complete. |
<U> CompletableFuture<U> |
applyToEitherAsync(CompletableFuture<? extends T> other,
CompletableFuture.Fun<? super T,U> fn,
Executor executor)
Creates and returns a CompletableFuture that is completed asynchronously
using the given executor with the result of the given function of either
this or the other given CompletableFuture's results when either complete.
|
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to complete 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. |
boolean |
completeExceptionally(Throwable ex)
If not already completed, causes invocations of
get() and
related methods to throw the given exception. |
CompletableFuture<T> |
exceptionally(CompletableFuture.Fun<Throwable,? extends T> fn)
Creates and returns a CompletableFuture that is completed 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 the computation to complete, and then retrieves
its result.
|
T |
get(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for completion, and then
retrieves its result, if available.
|
T |
getNow(T valueIfAbsent)
Returns the result value (or throws any encountered exception) if
completed, else returns the given valueIfAbsent.
|
<U> CompletableFuture<U> |
handle(CompletableFuture.BiFun<? super T,Throwable,? extends U> fn)
Creates and returns a CompletableFuture that is completed with the result
of the given function of the result and exception of this
CompletableFuture's completion when it completes.
|
boolean |
isCancelled()
Returns
true if this CompletableFuture was cancelled before it
completed normally. |
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(CompletableFuture<?> other,
Runnable action)
Creates and returns a CompletableFuture that is completed when this and
the other given CompletableFuture both complete.
|
CompletableFuture<Void> |
runAfterBothAsync(CompletableFuture<?> other,
Runnable action)
Creates and returns a CompletableFuture that is completed asynchronously
using the
ForkJoinPool.commonPool() when this and the other given
CompletableFuture both complete. |
CompletableFuture<Void> |
runAfterBothAsync(CompletableFuture<?> other,
Runnable action,
Executor executor)
Creates and returns a CompletableFuture that is completed asynchronously
using the given executor when this and the other given CompletableFuture
both complete.
|
CompletableFuture<Void> |
runAfterEither(CompletableFuture<?> other,
Runnable action)
Creates and returns a CompletableFuture that is completed after this or
the other given CompletableFuture complete.
|
CompletableFuture<Void> |
runAfterEitherAsync(CompletableFuture<?> other,
Runnable action)
Creates and returns a CompletableFuture that is completed asynchronously
using the
ForkJoinPool.commonPool() after this or the other given
CompletableFuture complete. |
CompletableFuture<Void> |
runAfterEitherAsync(CompletableFuture<?> other,
Runnable action,
Executor executor)
Creates and returns a CompletableFuture that is completed asynchronously
using the given executor after this or the other given CompletableFuture
complete.
|
static CompletableFuture<Void> |
runAsync(Runnable runnable)
Asynchronously executes in the
ForkJoinPool.commonPool() a task that runs the given action, and then
completes the returned CompletableFuture. |
static CompletableFuture<Void> |
runAsync(Runnable runnable,
Executor executor)
Asynchronously executes using the given executor, a task that runs the
given action, and then completes the returned CompletableFuture.
|
static <U> CompletableFuture<U> |
supplyAsync(CompletableFuture.Generator<U> supplier)
Asynchronously executes in the
ForkJoinPool.commonPool(), a task that completes the returned
CompletableFuture with the result of the given Supplier. |
static <U> CompletableFuture<U> |
supplyAsync(CompletableFuture.Generator<U> supplier,
Executor executor)
Asynchronously executes using the given executor, a task that completes
the returned CompletableFuture with the result of the given Supplier.
|
CompletableFuture<Void> |
thenAccept(CompletableFuture.Action<? super T> block)
Creates and returns a CompletableFuture that is completed after
performing the given action with this CompletableFuture's result when it
completes.
|
CompletableFuture<Void> |
thenAcceptAsync(CompletableFuture.Action<? super T> block)
Creates and returns a CompletableFuture that is asynchronously completed
using the
ForkJoinPool.commonPool() with this CompletableFuture's
result when it completes. |
CompletableFuture<Void> |
thenAcceptAsync(CompletableFuture.Action<? super T> block,
Executor executor)
Creates and returns a CompletableFuture that is asynchronously completed
using the given executor with this CompletableFuture's result when it
completes.
|
<U> CompletableFuture<Void> |
thenAcceptBoth(CompletableFuture<? extends U> other,
CompletableFuture.BiAction<? super T,? super U> block)
Creates and returns a CompletableFuture that is completed with the
results of this and the other given CompletableFuture if both complete.
|
<U> CompletableFuture<Void> |
thenAcceptBothAsync(CompletableFuture<? extends U> other,
CompletableFuture.BiAction<? super T,? super U> block)
Creates and returns a CompletableFuture that is completed asynchronously
using the
ForkJoinPool.commonPool() with the results of this and
the other given CompletableFuture when both complete. |
<U> CompletableFuture<Void> |
thenAcceptBothAsync(CompletableFuture<? extends U> other,
CompletableFuture.BiAction<? super T,? super U> block,
Executor executor)
Creates and returns a CompletableFuture that is completed asynchronously
using the given executor with the results of this and the other given
CompletableFuture when both complete.
|
<U> CompletableFuture<U> |
thenApply(CompletableFuture.Fun<? super T,? extends U> fn)
Creates and returns a CompletableFuture that is completed with the result
of the given function of this CompletableFuture.
|
<U> CompletableFuture<U> |
thenApplyAsync(CompletableFuture.Fun<? super T,? extends U> fn)
Creates and returns a CompletableFuture that is asynchronously completed
using the
ForkJoinPool.commonPool() with the result of the given
function of this CompletableFuture. |
<U> CompletableFuture<U> |
thenApplyAsync(CompletableFuture.Fun<? super T,? extends U> fn,
Executor executor)
Creates and returns a CompletableFuture that is asynchronously completed
using the given executor with the result of the given function of this
CompletableFuture.
|
<U,V> CompletableFuture<V> |
thenCombine(CompletableFuture<? extends U> other,
CompletableFuture.BiFun<? super T,? super U,? extends V> fn)
Creates and returns a CompletableFuture that is completed with the result
of the given function of this and the other given CompletableFuture's
results when both complete.
|
<U,V> CompletableFuture<V> |
thenCombineAsync(CompletableFuture<? extends U> other,
CompletableFuture.BiFun<? super T,? super U,? extends V> fn)
Creates and returns a CompletableFuture that is asynchronously completed
using the
ForkJoinPool.commonPool() with the result of the given
function of this and the other given CompletableFuture's results when
both complete. |
<U,V> CompletableFuture<V> |
thenCombineAsync(CompletableFuture<? extends U> other,
CompletableFuture.BiFun<? super T,? super U,? extends V> fn,
Executor executor)
Creates and returns a CompletableFuture that is asynchronously completed
using the given executor with the result of the given function of this
and the other given CompletableFuture's results when both complete.
|
<U> CompletableFuture<U> |
thenCompose(CompletableFuture.Fun<? super T,CompletableFuture<U>> fn)
Returns a CompletableFuture (or an equivalent one) produced by the given
function of the result of this CompletableFuture when completed.
|
CompletableFuture<Void> |
thenRun(Runnable action)
Creates and returns a CompletableFuture that is completed after
performing the given action when this CompletableFuture completes.
|
CompletableFuture<Void> |
thenRunAsync(Runnable action)
Creates and returns a CompletableFuture that is asynchronously completed
using the
ForkJoinPool.commonPool() after performing the given
action when this CompletableFuture completes. |
CompletableFuture<Void> |
thenRunAsync(Runnable action,
Executor executor)
Creates and returns a CompletableFuture that is asynchronously completed
using the given executor after performing the given action when this
CompletableFuture completes.
|
public static <U> CompletableFuture<U> supplyAsync(CompletableFuture.Generator<U> supplier)
ForkJoinPool.commonPool(), a task that completes the returned
CompletableFuture with the result of the given Supplier.supplier - a function returning the value to be used to complete the
returned CompletableFuturepublic static <U> CompletableFuture<U> supplyAsync(CompletableFuture.Generator<U> supplier, Executor executor)
supplier - 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)
ForkJoinPool.commonPool() a task that runs the given action, and then
completes the returned CompletableFuture.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 boolean isDone()
true if completed in any fashion: normally,
exceptionally, or via cancellation.public T get() throws InterruptedException, ExecutionException
get 在接口中 Future<T>CancellationException - if the computation was cancelledExecutionException - if the computation threw an exceptionInterruptedException - 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 the computation was cancelledExecutionException - if the computation threw an exceptionInterruptedException - if the current thread was interrupted while
waitingTimeoutException - if the wait timed outpublic T join()
CompletionException with the underlying exception as
its cause.CancellationException - if the computation was cancelledCompletionException - if a completion computation threw an
exceptionpublic T getNow(T valueIfAbsent)
valueIfAbsent - the value to return if not completedCancellationException - if the computation was cancelledCompletionException - if a completion computation threw an
exceptionpublic boolean complete(T value)
get() and related methods to the given value.value - the result valuetrue if this invocation caused this CompletableFuture to
transition to a completed state, else falsepublic boolean completeExceptionally(Throwable ex)
get() and
related methods to throw the given exception.ex - the exceptiontrue if this invocation caused this CompletableFuture to
transition to a completed state, else falsepublic <U> CompletableFuture<U> thenApply(CompletableFuture.Fun<? super T,? extends U> fn)
fn - the function to use to compute the value of the returned
CompletableFuturepublic <U> CompletableFuture<U> thenApplyAsync(CompletableFuture.Fun<? super T,? extends U> fn)
ForkJoinPool.commonPool() with the result of the given
function of this CompletableFuture. If this CompletableFuture completes
exceptionally, then the returned CompletableFuture also does so, with a
CompletionException holding this exception as its cause.fn - the function to use to compute the value of the returned
CompletableFuturepublic <U> CompletableFuture<U> thenApplyAsync(CompletableFuture.Fun<? super T,? extends U> fn, Executor executor)
fn - the function to use to compute the value of the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> thenAccept(CompletableFuture.Action<? super T> block)
block - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> thenAcceptAsync(CompletableFuture.Action<? super T> block)
ForkJoinPool.commonPool() with this CompletableFuture's
result when it completes. If this CompletableFuture completes
exceptionally, then the returned CompletableFuture also does so, with a
CompletionException holding this exception as its cause.block - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> thenAcceptAsync(CompletableFuture.Action<? super T> block, Executor executor)
block - the action to perform before completing the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> thenRun(Runnable action)
action - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> thenRunAsync(Runnable action)
ForkJoinPool.commonPool() after performing the given
action when this CompletableFuture completes. If this CompletableFuture
completes exceptionally, then the returned CompletableFuture also does
so, with a CompletionException holding this exception as its cause.action - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
action - the action to perform before completing the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic <U,V> CompletableFuture<V> thenCombine(CompletableFuture<? extends U> other, CompletableFuture.BiFun<? super T,? super U,? extends V> fn)
other - the other CompletableFuturefn - the function to use to compute the value of the returned
CompletableFuturepublic <U,V> CompletableFuture<V> thenCombineAsync(CompletableFuture<? extends U> other, CompletableFuture.BiFun<? super T,? super U,? extends V> fn)
ForkJoinPool.commonPool() with the result of the given
function of this and the other given CompletableFuture's results when
both complete. If this or the other CompletableFuture complete
exceptionally, then the returned CompletableFuture also does so, with a
CompletionException holding the exception as its cause.other - the other CompletableFuturefn - the function to use to compute the value of the returned
CompletableFuturepublic <U,V> CompletableFuture<V> thenCombineAsync(CompletableFuture<? extends U> other, CompletableFuture.BiFun<? super T,? super U,? extends V> fn, Executor executor)
other - the other CompletableFuturefn - the function to use to compute the value of the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic <U> CompletableFuture<Void> thenAcceptBoth(CompletableFuture<? extends U> other, CompletableFuture.BiAction<? super T,? super U> block)
other - the other CompletableFutureblock - the action to perform before completing the returned
CompletableFuturepublic <U> CompletableFuture<Void> thenAcceptBothAsync(CompletableFuture<? extends U> other, CompletableFuture.BiAction<? super T,? super U> block)
ForkJoinPool.commonPool() with the results of this and
the other given CompletableFuture when both complete. If this and/or the
other CompletableFuture complete exceptionally, then the returned
CompletableFuture also does so, with a CompletionException holding one of
these exceptions as its cause.other - the other CompletableFutureblock - the action to perform before completing the returned
CompletableFuturepublic <U> CompletableFuture<Void> thenAcceptBothAsync(CompletableFuture<? extends U> other, CompletableFuture.BiAction<? super T,? super U> block, Executor executor)
other - the other CompletableFutureblock - the action to perform before completing the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> runAfterBoth(CompletableFuture<?> other, Runnable action)
other - the other CompletableFutureaction - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> runAfterBothAsync(CompletableFuture<?> other, Runnable action)
ForkJoinPool.commonPool() when this and the other given
CompletableFuture both complete. If this and/or the other
CompletableFuture complete exceptionally, then the returned
CompletableFuture also does so, with a CompletionException holding one of
these exceptions as its cause.other - the other CompletableFutureaction - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> runAfterBothAsync(CompletableFuture<?> other, Runnable action, Executor executor)
other - the other CompletableFutureaction - the action to perform before completing the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic <U> CompletableFuture<U> applyToEither(CompletableFuture<? extends T> other, CompletableFuture.Fun<? super T,U> fn)
other - the other CompletableFuturefn - the function to use to compute the value of the returned
CompletableFuturepublic <U> CompletableFuture<U> applyToEitherAsync(CompletableFuture<? extends T> other, CompletableFuture.Fun<? super T,U> fn)
ForkJoinPool.commonPool() with the result of the given
function of either this or the other given CompletableFuture's results
when either complete. If this and/or the other CompletableFuture complete
exceptionally, then the returned CompletableFuture may also do so, with a
CompletionException holding one of these exceptions as its cause. No
guarantees are made about which result or exception is used in the
returned CompletableFuture.other - the other CompletableFuturefn - the function to use to compute the value of the returned
CompletableFuturepublic <U> CompletableFuture<U> applyToEitherAsync(CompletableFuture<? extends T> other, CompletableFuture.Fun<? super T,U> fn, Executor executor)
other - the other CompletableFuturefn - the function to use to compute the value of the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> acceptEither(CompletableFuture<? extends T> other, CompletableFuture.Action<? super T> block)
other - the other CompletableFutureblock - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> acceptEitherAsync(CompletableFuture<? extends T> other, CompletableFuture.Action<? super T> block)
ForkJoinPool.commonPool(), performing the given action
with the result of either this or the other given CompletableFuture's
result, when either complete. If this and/or the other CompletableFuture
complete exceptionally, then the returned CompletableFuture may also do
so, with a CompletionException holding one of these exceptions as its
cause. No guarantees are made about which exception is used in the
returned CompletableFuture.other - the other CompletableFutureblock - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> acceptEitherAsync(CompletableFuture<? extends T> other, CompletableFuture.Action<? super T> block, Executor executor)
other - the other CompletableFutureblock - the action to perform before completing the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic CompletableFuture<Void> runAfterEither(CompletableFuture<?> other, Runnable action)
other - the other CompletableFutureaction - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> runAfterEitherAsync(CompletableFuture<?> other, Runnable action)
ForkJoinPool.commonPool() after this or the other given
CompletableFuture complete. If this and/or the other CompletableFuture
complete exceptionally, then the returned CompletableFuture may also do
so, with a CompletionException holding one of these exceptions as its
cause. No guarantees are made about which exception is used in the
returned CompletableFuture.other - the other CompletableFutureaction - the action to perform before completing the returned
CompletableFuturepublic CompletableFuture<Void> runAfterEitherAsync(CompletableFuture<?> other, Runnable action, Executor executor)
other - the other CompletableFutureaction - the action to perform before completing the returned
CompletableFutureexecutor - the executor to use for asynchronous executionpublic <U> CompletableFuture<U> thenCompose(CompletableFuture.Fun<? super T,CompletableFuture<U>> fn)
fn - the function returning a new CompletableFutureisDone() upon return if
completed by the given function, or an exception occurspublic CompletableFuture<T> exceptionally(CompletableFuture.Fun<Throwable,? extends T> fn)
fn - the function to use to compute the value of the returned
CompletableFuture if this CompletableFuture completed exceptionallypublic <U> CompletableFuture<U> handle(CompletableFuture.BiFun<? super T,Throwable,? extends U> fn)
null if none) and the exception (or
null if none) of this CompletableFuture when complete.fn - the function to use to compute the value of the returned
CompletableFuturepublic 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 void obtrudeValue(T value)
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.value - the completion valuepublic void obtrudeException(Throwable ex)
get() and
related methods to throw the given exception, whether or not already
completed. This method is designed for use only in recovery actions, and
even in such situations may result in ongoing dependent completions using
established versus overwritten outcomes.ex - the exceptionCopyright © 2013 The Skfiy Open Association. All Rights Reserved.