Class CompletableFuture<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
CompletionStage<T>,Future<T>
java.util.concurrent.CompletableFuture, which the class library does not carry.
Failures follow the JDK's wrapping: a stage that fails because of another is completed with a
CompletionException around the cause, join() throws that, and get()
unwraps it into an ExecutionException. supplyAsync(Supplier) runs the task on a
new fiber, since there is no pool, and callbacks run on the fiber that completes the stage.
get(), get(long, TimeUnit) and join() suspend the caller, so they
trap when no fiber is running, even on a future that is already done. The generated entry point
builds the handler, a Durable Object and a Workflow on a fiber, so their constructors and the
static initializers they trigger may call them.
When a whenComplete action throws on a stage that had already failed, the original
failure stands and the action's is dropped; the JDK adds it as a suppressed exception, and
Throwable.addSuppressed throws a NullPointerException on this runtime.
Left out: the *Async forms of the then methods, the *Either and
*Both forms, orTimeout, completeAsync, the obtrude methods and the
Java 19 additions. Calling one fails to link when the program is built.
- Since:
- 1.0.3
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) booleancancel(boolean mayInterruptIfRunning) booleanstatic <U> CompletableFuture<U> completedFuture(U value) booleanexceptionally(Function<Throwable, ? extends T> fn) static <U> CompletableFuture<U> get()Suspends the caller until the future is done.<U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) booleanReturns whether the future was cancelled.booleanReturns whether the future failed, including by cancellation.booleanisDone()Returns whether the future has a result, a failure or was cancelled.join()Suspends the caller until the future is done.static CompletableFuture<Void> static CompletableFuture<Void> static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) thenAccept(Consumer<? super T> action) <U> CompletableFuture<U> <U,V> CompletableFuture <V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) Returns this stage as aCompletableFuture.toString()whenComplete(BiConsumer<? super T, ? super Throwable> action)
-
Constructor Details
-
CompletableFuture
public CompletableFuture()A future nothing has completed.
-
-
Method Details
-
supplyAsync
- Type Parameters:
U- the value type- Parameters:
supplier- computes the value on a new fiber- Returns:
- the future its result completes
-
supplyAsync
- Type Parameters:
U- the value type- Parameters:
supplier- computes the valueexecutor- runs the task- Returns:
- the future its result completes
-
runAsync
- Parameters:
runnable- runs on a new fiber- Returns:
- the future that completes once it has run
-
runAsync
- Parameters:
runnable- runsexecutor- runs the task- Returns:
- the future that completes once it has run
-
completedFuture
- Type Parameters:
U- the value type- Parameters:
value- the value- Returns:
- a future already holding it
-
failedFuture
- Type Parameters:
U- the value type- Parameters:
ex- the failure- Returns:
- a future already failed with it
-
allOf
- Parameters:
cfs- the futures to wait for- Returns:
- a future that completes once all of them have, failing with the first failure in argument order
-
anyOf
- Parameters:
cfs- the futures to race- Returns:
- a future that completes with whichever finishes first, the first listed when several already have; one that never completes when none are given
-
complete
- Parameters:
value- the value- Returns:
- whether this call completed the future
-
completeExceptionally
- Parameters:
ex- the failure- Returns:
- whether this call completed the future
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) -
isDone
public boolean isDone()Returns whether the future has a result, a failure or was cancelled. -
isCancelled
public boolean isCancelled()Returns whether the future was cancelled.- Specified by:
isCancelledin interfaceFuture<T>- Returns:
- whether the future was cancelled
-
isCompletedExceptionally
public boolean isCompletedExceptionally()Returns whether the future failed, including by cancellation.- Returns:
- whether the future failed, including by cancellation
-
get
Suspends the caller until the future is done.- Specified by:
getin interfaceFuture<T>- Returns:
- the value
- Throws:
InterruptedException- if the caller was interrupted while waitingExecutionException- if the future failed, with the underlying failure as its cause
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
getin interfaceFuture<T>- Parameters:
timeout- the longest to waitunit- the unit oftimeout- Returns:
- the value
- Throws:
InterruptedException- if the caller was interrupted while waitingExecutionException- if the future failed, with the underlying failure as its causeTimeoutException- if the time ran out first
-
join
Suspends the caller until the future is done.- Returns:
- the value
- Throws:
CompletionException- if the future failed, with the underlying failure as its cause
-
getNow
- Parameters:
valueIfAbsent- returned when the future is not done- Returns:
- the value, or
valueIfAbsent
-
thenApply
- Specified by:
thenApplyin interfaceCompletionStage<T>- Type Parameters:
U- the mapped type- Parameters:
fn- maps the value- Returns:
- a stage holding the mapped value
-
thenAccept
- Specified by:
thenAcceptin interfaceCompletionStage<T>- Parameters:
action- takes the value- Returns:
- a stage that completes once the action has run
-
thenRun
- Specified by:
thenRunin interfaceCompletionStage<T>- Parameters:
action- runs once this stage completes normally- Returns:
- a stage that completes once the action has run
-
thenCombine
public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) - Specified by:
thenCombinein interfaceCompletionStage<T>- Type Parameters:
U- the second value typeV- the combined type- Parameters:
other- the second stagefn- combines both values- Returns:
- a stage holding the combined value
-
thenCompose
- Specified by:
thenComposein interfaceCompletionStage<T>- Type Parameters:
U- the adopted type- Parameters:
fn- returns the stage whose result this one adopts- Returns:
- a stage holding that result
-
exceptionally
- Specified by:
exceptionallyin interfaceCompletionStage<T>- Parameters:
fn- turns a failure into a value- Returns:
- a stage holding the value or the recovered one
-
handle
- Specified by:
handlein interfaceCompletionStage<T>- Type Parameters:
U- the mapped type- Parameters:
fn- maps the value and the failure, one of which is null- Returns:
- a stage holding the mapped value
-
whenComplete
- Specified by:
whenCompletein interfaceCompletionStage<T>- Parameters:
action- sees the value and the failure, one of which is null- Returns:
- a stage with this one's result
-
toCompletableFuture
Description copied from interface:CompletionStageReturns this stage as aCompletableFuture.- Specified by:
toCompletableFuturein interfaceCompletionStage<T>- Returns:
- this stage as a
CompletableFuture
-
toString
-