stage.thenApply(x -> square(x))
.thenAccept(x -> System.out.print(x))
.thenRun(() -> System.out.println());
An additional form (compose) allows the construction of
computation pipelines from functions returning completion stages.
Any argument to a stage's computation is the outcome of a triggering stage's computation.
Executor). The execution
properties of default and async modes are specified by
CompletionStage implementations, not this interface. Methods with
explicit Executor arguments may have arbitrary execution
properties, and might not even support concurrent execution, but
are arranged for processing in a way that accommodates asynchrony.
handle and whenComplete) support unconditional computation
whether the triggering stage completed normally or exceptionally.
Method exceptionally supports computation
only when the triggering stage completes exceptionally, computing a
replacement result, similarly to the java catch keyword.
In all other cases, if a stage's computation terminates abruptly
with an (unchecked) exception or error, then all dependent stages
requiring its completion complete exceptionally as well, with a
CompletionException holding the exception as its
cause. This convention distinguishes exceptions in an action itself
from those it depends on. If they are to be handled in the same
way, instead catch RuntimeException (possibly inspecting
the exception's getCause()). If a stage is dependent on
both of two stages, and both complete exceptionally, then
the CompletionException may correspond to either one of these
exceptions. If a stage is dependent on either of two
others, and only one of them completes exceptionally, no guarantees
are made about whether the dependent stage completes normally or
exceptionally. In the case of method whenComplete, when the
supplied action itself encounters an exception, then the stage
completes exceptionally with that exception unless the source stage
also completed exceptionally, in which case the exceptional
completion from the source stage is given preference and propagated
to the dependent stage. Applications are encouraged to maintain
these conventions, avoiding unnecessary nesting when rethrowing, as
in throw (ex instanceof CompletionException) ? ex : new
CompletionException(ex).
All methods adhere to the above triggering, execution, and
exceptional completion specifications (which are not repeated in
individual method specifications). Additionally, while arguments
used to pass a completion result (that is, for parameters of type
T) for methods accepting them may be null, passing a null
value for any other parameter will result in a NullPointerException being thrown.
Method form handle is the most general way of
creating a continuation stage, unconditionally performing a
computation that is given both the result and exception (if any) of
the triggering CompletionStage, and computing an arbitrary result.
Method whenComplete is similar, but preserves
the result of the triggering stage instead of computing a new one.
Because a stage's normal result may be null, both methods
should have a computation structured thus:
(result, exception) -> {
if (exception == null) {
// triggering stage completed normally
} else {
// triggering stage completed exceptionally
}
}
The CompletionStage interface does not define methods for initially creating,
forcibly completing normally or exceptionally, probing completion
status or results, or awaiting completion of a stage.
Implementations of CompletionStage may provide means of achieving
such effects, as appropriate. Method toCompletableFuture
enables interoperability among different implementations of this
interface by providing a common conversion type.
Memory consistency effects: Actions in a thread prior to the
submission of a computation producing a CompletionStage happen-before
that computation begins. And actions taken by CompletionStage x happen-before actions of any dependent
stage subsequent to x's completion.
| Modifier and Type | Method and Description |
|---|---|
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Consumer<? super T> the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when either |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Consumer<? super T> the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when either |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Consumer<? super T> the action to perform before completing the
returned CompletionStage action, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when either |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Function<? super T, U> the function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when either |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Function<? super T, U> the function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when either |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Function<? super T, U> the function to use to compute the value of the
returned CompletionStage fn, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when either |
| public CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage if fn)this CompletionStage completed
exceptionallyReturns a new CompletionStage that, when |
| public default CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage if fn)this CompletionStage completed
exceptionallyReturns a new CompletionStage that, when |
| public default CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage if fn, Executor this CompletionStage completed
exceptionallythe executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public default CompletionStage | Returns: the new CompletionStagethe function to use to compute the returned
CompletionStage if fn)this CompletionStage completed exceptionallyReturns a new CompletionStage that, when |
| public default CompletionStage | Returns: the new CompletionStagethe function to use to compute the returned
CompletionStage if fn)this CompletionStage completed exceptionallyReturns a new CompletionStage that, when |
| public default CompletionStage | Returns: the new CompletionStagethe function to use to compute the returned
CompletionStage if fn, Executor this CompletionStage completed exceptionallythe executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage fn, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Runnable the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Runnable the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Runnable the action to perform before completing the
returned CompletionStage action, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Runnable the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when either |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Runnable the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when either |
| public CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, Runnable the action to perform before completing the
returned CompletionStage action, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when either |
| public CompletionStage | Returns: the new CompletionStagethe action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when |
| public CompletionStage | Returns: the new CompletionStagethe action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when |
| public CompletionStage | Returns: the new CompletionStagethe action to perform before completing the
returned CompletionStage action, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public < the type of the other CompletionStage's result U> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, BiConsumer<? super T, ? super U> the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when |
| public < the type of the other CompletionStage's result U> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, BiConsumer<? super T, ? super U> the action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when |
| public < the type of the other CompletionStage's result U> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, BiConsumer<? super T, ? super U> the action to perform before completing the
returned CompletionStage action, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when |
| public < the function's return type U> CompletionStage | Returns: the new CompletionStagethe function to use to compute the value of the
returned CompletionStage fn, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public < the type of the other CompletionStage's result U, the function's return type V> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, BiFunction<? super T, ? super U, ? extends V> the function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when |
| public < the type of the other CompletionStage's result U, the function's return type V> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, BiFunction<? super T, ? super U, ? extends V> the function to use to compute the value of the
returned CompletionStage fn)Returns a new CompletionStage that, when |
| public < the type of the other CompletionStage's result U, the function's return type V> CompletionStage | Returns: the new CompletionStagethe other CompletionStage other, BiFunction<? super T, ? super U, ? extends V> the function to use to compute the value of the
returned CompletionStage fn, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public < the type of the returned CompletionStage's result U> CompletionStage | Returns: the new CompletionStagethe function to use to compute another CompletionStage fn)Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function. |
| public < the type of the returned CompletionStage's result U> CompletionStage | Returns: the new CompletionStagethe function to use to compute another CompletionStage fn)Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function,
executed using |
| public < the type of the returned CompletionStage's result U> CompletionStage | Returns: the new CompletionStagethe function to use to compute another CompletionStage fn, Executor the executor to use for asynchronous execution 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. |
| public CompletionStage | |
| public CompletionStage | Returns: the new CompletionStagethe action to perform before completing the
returned CompletionStage action)Returns a new CompletionStage that, when |
| public CompletionStage | Returns: the new CompletionStagethe action to perform before completing the
returned CompletionStage action, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage that, when |
| public CompletableFuture | Returns: the CompletableFutureReturns a |
| public CompletionStage | Returns: the new CompletionStagethe action to perform action)Returns a new CompletionStage with the same result or exception as
|
| public CompletionStage | Returns: the new CompletionStagethe action to perform action)Returns a new CompletionStage with the same result or exception as
|
| public CompletionStage | Returns: the new CompletionStagethe action to perform action, Executor the executor to use for asynchronous execution executor)Returns a new CompletionStage with the same result or exception as
|