|
FlexDoc/Javadoc 2.0 Demo Java Doc |
When two or more threads attempt to complete, completeExceptionally, or cancel a CompletableFuture, only one of them succeeds.
In addition to these and related methods for directly manipulating status and results, CompletableFuture implements interface CompletionStage with the following policies:
CompletableFuture also implements Future with the following policies:
Arguments used to pass a completion result (that is, for parameters of type T) for methods accepting them may be null, but passing a null value for any other parameter will result in a NullPointerException being thrown.
Subclasses of this class should normally override the "virtual constructor" method newIncompleteFuture(), which establishes the concrete type returned by CompletionStage methods. For example, here is a class that substitutes a different default Executor and disables the obtrude methods:
class MyCompletableFuture<T> extends CompletableFuture<T> {
static final Executor myExecutor = ...;
public MyCompletableFuture() { }
public <U> CompletableFuture<U> newIncompleteFuture() {
return new MyCompletableFuture<U>(); }
public Executor defaultExecutor() {
return myExecutor; }
public void obtrudeValue(T value) {
throw new UnsupportedOperationException(); }
public void obtrudeException(Throwable ex) {
throw new UnsupportedOperationException(); }
}
Nested Class Summary |
||
static interface |
A marker interface identifying asynchronous tasks produced by
async methods.
|
Constructor Summary |
||
Creates a new incomplete CompletableFuture.
|
Method Summary |
||
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.
|
||
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.
|
||
acceptEitherAsync(CompletionStage<? 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.
|
|
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.
|
||
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.
|
||
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 |
If not already completed, sets the value returned by get() and related methods to the given value.
|
|
Completes this CompletableFuture with the result of the given
Supplier function invoked from an asynchronous task using the
default executor.
|
||
Completes this CompletableFuture with the result of
the given Supplier function invoked from an asynchronous
task using the given executor.
|
||
completedFuture(U value)
Returns a new CompletableFuture that is already completed with
the given value.
|
||
completedStage(U value)
Returns a new CompletionStage that is already completed with
the given value and supports only those methods in
interface CompletionStage.
|
||
boolean |
If not already completed, causes invocations of get()
and related methods to throw the given exception.
|
|
Completes this CompletableFuture with the given value if not
otherwise completed before the given timeout.
|
||
copy()
Returns a new CompletableFuture that is completed normally with
the same value as this CompletableFuture when it completes
normally.
|
||
Returns the default Executor used for async methods that do not
specify an Executor.
|
||
static Executor |
delayedExecutor(long delay, TimeUnit unit)
Returns a new Executor that submits a task to the default
executor after the given delay (or no delay if non-positive).
|
|
static Executor |
Returns a new Executor that submits a task to the given base
executor after the given delay (or no delay if non-positive).
|
|
Returns a new CompletionStage that, when this stage completes
exceptionally, is executed with this stage's exception as the
argument to the supplied function.
|
||
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.
|
||
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.
|
||
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.
|
||
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.
|
||
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.
|
||
Returns a new CompletableFuture that is already completed
exceptionally with the given exception.
|
||
failedStage(Throwable ex)
Returns a new CompletionStage that is already completed
exceptionally with the given exception and supports only those
methods in interface CompletionStage.
|
||
get()
Waits if necessary for this future to complete, and then
returns its result.
|
||
Waits if necessary for at most the given time for this future
to complete, and then returns its result, if available.
|
||
Returns the result value (or throws any encountered exception)
if completed, else returns the given valueIfAbsent.
|
||
int |
Returns the estimated number of CompletableFutures whose
completions are awaiting completion of this CompletableFuture.
|
|
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.
|
||
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.
|
||
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 |
Returns true if this CompletableFuture was cancelled
before it completed normally.
|
|
boolean |
Returns true if this CompletableFuture completed
exceptionally, in any way.
|
|
boolean |
isDone()
Returns true if completed in any fashion: normally,
exceptionally, or via cancellation.
|
|
join()
Returns the result value when complete, or throws an
(unchecked) exception if completed exceptionally.
|
||
Returns a new CompletionStage that is completed normally with
the same value as this CompletableFuture when it completes
normally, and cannot be independently completed or otherwise
used in ways not defined by the methods of interface CompletionStage.
|
||
Returns a new incomplete CompletableFuture of the type to be
returned by a CompletionStage method.
|
||
void |
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.
|
|
Exceptionally completes this CompletableFuture with
a TimeoutException if not otherwise completed
before the given timeout.
|
||
Returns a new CompletionStage that, when this and the other
given stage both complete normally, executes the given 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.
|
||
Returns a new CompletionStage that, when this and the other
given stage both complete normally, executes the given action
using the supplied executor.
|
||
Returns a new CompletionStage that, when either this or the
other given stage complete normally, executes the given 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.
|
||
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> |
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> |
Returns a new CompletableFuture that is asynchronously completed
by a task running in the given executor after it runs the given
action.
|
|
Returns a new CompletableFuture that is asynchronously completed
by a task running in the ForkJoinPool.commonPool() with
the value obtained by calling the given Supplier.
|
||
Returns a new CompletableFuture that is asynchronously completed
by a task running in the given executor with the value obtained
by calling the given Supplier.
|
||
Returns a new CompletionStage that, when this stage completes
normally, is executed with this stage's result as the argument
to the supplied 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.
|
||
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.
|
||
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.
|
||
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.
|
||
thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? 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.
|
||
Returns a new CompletionStage that, when this stage completes
normally, is executed with this stage's result as the argument
to the supplied function.
|
||
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.
|
||
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.
|
||
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.
|
||
thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? 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.
|
||
thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? 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.
|
||
Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function.
|
||
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.
|
||
Returns a new CompletionStage that is completed with the same
value as the CompletionStage returned by the given function,
executed using the supplied Executor.
|
||
Returns a new CompletionStage that, when this stage completes
normally, executes the given action.
|
||
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.
|
||
Returns a new CompletionStage that, when this stage completes
normally, executes the given action using the supplied Executor.
|
||
Returns this CompletableFuture.
|
||
toString()
Returns a string identifying this CompletableFuture, as well as
its completion state.
|
||
Returns a new CompletionStage with the same result or exception as
this stage, that executes the given action when this stage completes.
|
||
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.
|
||
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.
|
Methods inherited from class java.lang.Object |
public CompletableFuture |
() |
(Runnable runnable) |
(U value) |
public boolean isDone |
() |
public T get |
() |
throws |
public T get |
(long timeout, TimeUnit unit) |
throws |
public T join |
() |
public T getNow |
(T valueIfAbsent) |
public boolean complete |
(T value) |
public boolean completeExceptionally |
(Throwable ex) |
This method is analogous to Optional.map and Stream.map.
See the CompletionStage documentation for rules covering exceptional completion.
(Runnable action) |
(Runnable action) |
(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor) |
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 Optional.flatMap and Stream.flatMap.
See the CompletionStage documentation for rules covering exceptional completion.
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 CompletionStage documentation for rules covering exceptional completion.
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 CompletionStage documentation for rules covering exceptional completion.
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, 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.
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, 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.
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, 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.
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.
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.
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.
() |
(CompletableFuture<?>... cfs) |
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();.
(CompletableFuture<?>... cfs) |
public boolean cancel |
(boolean mayInterruptIfRunning) |
public boolean isCancelled |
() |
public boolean isCompletedExceptionally |
() |
public void obtrudeValue |
(T value) |
public void obtrudeException |
(Throwable ex) |
public int getNumberOfDependents |
() |
public String toString |
() |
() |
public Executor defaultExecutor |
() |
() |
() |
Unless overridden by a subclass, a new non-minimal CompletableFuture with all methods available can be obtained from a minimal CompletionStage via toCompletableFuture(). For example, completion of a minimal stage can be awaited by
minimalStage.toCompletableFuture().join();
(long timeout, TimeUnit unit) |
public static Executor delayedExecutor |
public static Executor delayedExecutor |
(long delay, TimeUnit unit) |
(U value) |
(Throwable ex) |
(Throwable ex) |
|
FlexDoc/Javadoc 2.0 Demo Java Doc |