APIdia Launch Partner
Top Description Methods
java.util.concurrent

public Interface CompletionStage<T>

Known Direct Implementers
java.util.concurrent.CompletableFuture
Type Parameters
<T>
the type of values the stage produces or consumes
Imports
java.util.function.BiConsumer, .BiFunction, .Consumer, .Function

A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes. A stage completes upon termination of its computation, which may in turn trigger other dependent stages. The functionality defined in this interface takes only a few basic forms, which expand out to a larger set of methods to capture a range of usage styles:

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.

Author
Doug Lea
Since
1.8

Method Summary

Modifier and TypeMethod and Description
public CompletionStage<Void>

Returns:

the new CompletionStage
acceptEither
(CompletionStage<? extends T>
the other CompletionStage
other
,
Consumer<? super T>
the action to perform before completing the returned CompletionStage
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.

public CompletionStage<Void>

Returns:

the new CompletionStage
acceptEitherAsync
(CompletionStage<? extends T>
the other CompletionStage
other
,
Consumer<? super T>
the action to perform before completing the returned CompletionStage
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.

public CompletionStage<Void>

Returns:

the new CompletionStage
acceptEitherAsync
(CompletionStage<? extends T>
the 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 this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied action.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
applyToEither
(CompletionStage<? extends T>
the 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 this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied function.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
applyToEitherAsync
(CompletionStage<? extends T>
the 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 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.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
applyToEitherAsync
(CompletionStage<? extends T>
the 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 this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied function.

public CompletionStage<T>

Returns:

the new CompletionStage
exceptionally
(Function<Throwable, ? extends T>
the function to use to compute the value of the returned CompletionStage if this CompletionStage completed exceptionally
fn
)

Returns a new CompletionStage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function.

public default CompletionStage<T>

Returns:

the new CompletionStage
exceptionallyAsync
(Function<Throwable, ? extends T>
the function to use to compute the value of the returned CompletionStage if this CompletionStage completed exceptionally
fn
)

Returns a new CompletionStage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function, using this stage's default asynchronous execution facility.

public default CompletionStage<T>

Returns:

the new CompletionStage
exceptionallyAsync
(Function<Throwable, ? extends T>
the function to use to compute the value of the returned CompletionStage if this CompletionStage completed exceptionally
fn
,
Executor
the executor to use for asynchronous execution
executor
)

Returns a new CompletionStage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function, using the supplied Executor.

public default CompletionStage<T>

Returns:

the new CompletionStage
exceptionallyCompose
(Function<Throwable, ? extends CompletionStage<T>>
the function to use to compute the returned CompletionStage if this CompletionStage completed exceptionally
fn
)

Returns a new CompletionStage that, when this stage completes exceptionally, is composed using the results of the supplied function applied to this stage's exception.

public default CompletionStage<T>

Returns:

the new CompletionStage
exceptionallyComposeAsync
(Function<Throwable, ? extends CompletionStage<T>>
the function to use to compute the returned CompletionStage if this CompletionStage completed exceptionally
fn
)

Returns a new CompletionStage that, when this stage completes exceptionally, is composed using the results of the supplied function applied to this stage's exception, using this stage's default asynchronous execution facility.

public default CompletionStage<T>

Returns:

the new CompletionStage
exceptionallyComposeAsync
(Function<Throwable, ? extends CompletionStage<T>>
the function to use to compute the returned CompletionStage if this CompletionStage completed exceptionally
fn
,
Executor
the executor to use for asynchronous execution
executor
)

Returns a new CompletionStage that, when this stage completes exceptionally, is composed using the results of the supplied function applied to this stage's exception, using the supplied Executor.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
handle
(BiFunction<? super T, Throwable, ? extends U>
the function to use to compute the value of the returned CompletionStage
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.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
handleAsync
(BiFunction<? super T, Throwable, ? extends U>
the function to use to compute the value of the returned CompletionStage
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.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
handleAsync
(BiFunction<? super T, Throwable, ? extends 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 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.

public CompletionStage<Void>

Returns:

the new CompletionStage
runAfterBoth
(CompletionStage<?>
the other CompletionStage
other
,
Runnable
the action to perform before completing the returned CompletionStage
action
)

Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action.

public CompletionStage<Void>

Returns:

the new CompletionStage
runAfterBothAsync
(CompletionStage<?>
the other CompletionStage
other
,
Runnable
the action to perform before completing the returned CompletionStage
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.

public CompletionStage<Void>

Returns:

the new CompletionStage
runAfterBothAsync
(CompletionStage<?>
the 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 this and the other given stage both complete normally, executes the given action using the supplied executor.

public CompletionStage<Void>

Returns:

the new CompletionStage
runAfterEither
(CompletionStage<?>
the other CompletionStage
other
,
Runnable
the action to perform before completing the returned CompletionStage
action
)

Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action.

public CompletionStage<Void>

Returns:

the new CompletionStage
runAfterEitherAsync
(CompletionStage<?>
the other CompletionStage
other
,
Runnable
the action to perform before completing the returned CompletionStage
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.

public CompletionStage<Void>

Returns:

the new CompletionStage
runAfterEitherAsync
(CompletionStage<?>
the 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 this or the other given stage complete normally, executes the given action using the supplied executor.

public CompletionStage<Void>

Returns:

the new CompletionStage
thenAccept
(Consumer<? super T>
the action to perform before completing the returned CompletionStage
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.

public CompletionStage<Void>

Returns:

the new CompletionStage
thenAcceptAsync
(Consumer<? super T>
the action to perform before completing the returned CompletionStage
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.

public CompletionStage<Void>

Returns:

the new CompletionStage
thenAcceptAsync
(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 this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied action.

public <
the type of the other CompletionStage's result
U
>
CompletionStage<Void>

Returns:

the new CompletionStage
thenAcceptBoth
(CompletionStage<? extends U>
the other CompletionStage
other
,
BiConsumer<? super T, ? super U>
the action to perform before completing the returned CompletionStage
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.

public <
the type of the other CompletionStage's result
U
>
CompletionStage<Void>

Returns:

the new CompletionStage
thenAcceptBothAsync
(CompletionStage<? extends U>
the other CompletionStage
other
,
BiConsumer<? super T, ? super U>
the action to perform before completing the returned CompletionStage
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.

public <
the type of the other CompletionStage's result
U
>
CompletionStage<Void>

Returns:

the new CompletionStage
thenAcceptBothAsync
(CompletionStage<? extends U>
the 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 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.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
thenApply
(Function<? super T, ? extends U>
the function to use to compute the value of the returned CompletionStage
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.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
thenApplyAsync
(Function<? super T, ? extends U>
the function to use to compute the value of the returned CompletionStage
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.

public <
the function's return type
U
>
CompletionStage<U>

Returns:

the new CompletionStage
thenApplyAsync
(Function<? super T, ? extends 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 this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied function.

public <
the type of the other CompletionStage's result
U
,
the function's return type
V
>
CompletionStage<V>

Returns:

the new CompletionStage
thenCombine
(CompletionStage<? extends U>
the 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 this and the other given stage both complete normally, is executed with the two results as arguments to the supplied function.

public <
the type of the other CompletionStage's result
U
,
the function's return type
V
>
CompletionStage<V>

Returns:

the new CompletionStage
thenCombineAsync
(CompletionStage<? extends U>
the 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 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.

public <
the type of the other CompletionStage's result
U
,
the function's return type
V
>
CompletionStage<V>

Returns:

the new CompletionStage
thenCombineAsync
(CompletionStage<? extends U>
the 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 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.

public <
the type of the returned CompletionStage's result
U
>
CompletionStage<U>

Returns:

the new CompletionStage
thenCompose
(Function<? super T, ? extends CompletionStage<U>>
the 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<U>

Returns:

the new CompletionStage
thenComposeAsync
(Function<? super T, ? extends CompletionStage<U>>
the 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 this stage's default asynchronous execution facility.

public <
the type of the returned CompletionStage's result
U
>
CompletionStage<U>

Returns:

the new CompletionStage
thenComposeAsync
(Function<? super T, ? extends CompletionStage<U>>
the 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<Void>

Returns:

the new CompletionStage
thenRun
(Runnable
the action to perform before completing the returned CompletionStage
action
)

Returns a new CompletionStage that, when this stage completes normally, executes the given action.

public CompletionStage<Void>

Returns:

the new CompletionStage
thenRunAsync
(Runnable
the action to perform before completing the returned CompletionStage
action
)

Returns a new CompletionStage that, when this stage completes normally, executes the given action using this stage's default asynchronous execution facility.

public CompletionStage<Void>

Returns:

the new CompletionStage
thenRunAsync
(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 this stage completes normally, executes the given action using the supplied Executor.

public CompletableFuture<T>

Returns:

the CompletableFuture
toCompletableFuture
()

Returns a CompletableFuture maintaining the same completion properties as this stage.

public CompletionStage<T>

Returns:

the new CompletionStage
whenComplete
(BiConsumer<? super T, ? super Throwable>
the action to perform
action
)

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action when this stage completes.

public CompletionStage<T>

Returns:

the new CompletionStage
whenCompleteAsync
(BiConsumer<? super T, ? super Throwable>
the action to perform
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.

public CompletionStage<T>

Returns:

the new CompletionStage
whenCompleteAsync
(BiConsumer<? super T, ? super Throwable>
the action to perform
action
,
Executor
the executor to use for asynchronous execution
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.