Class CompletableFuture<T>
- All Implemented Interfaces:
CompletionStage<T>, Future<T>
java.util.concurrent.CompletableFuture for the Codename One runtime.
A stage completes on the thread that completed the one before it, as in
the JDK. The ...Async methods without an executor run on a pool of
daemon threads -- there is no fork-join pool here -- and never on the UI
thread; a continuation that touches the user interface passes an executor
that calls Display.callSerially, or calls it itself.
The class is safe to use from several threads at once: its state is kept in atomics, and every dependent action runs exactly once.
get() and join() on the UI thread wait through JdkHost.blockUi,
which on a device is Display.invokeAndBlock: events keep being
dispatched.
Exceptions travel as in the JDK: a stage that depends on a failed one
fails with a CompletionException whose cause is the original; get()
reports that cause in an ExecutionException, join() throws the
CompletionException, and both throw CancellationException after
cancel.
Not provided: obtrudeValue, obtrudeException,
getNumberOfDependents, delayedExecutor and state().
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionacceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) static CompletableFuture<Void> allOf(CompletableFuture<?>... given) static CompletableFuture<Object> anyOf(CompletableFuture<?>... futures) <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn) <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor) booleancancel(boolean mayInterruptIfRunning) Stops the work if it has not started.booleancompleteAsync(Supplier<? extends T> supplier) completeAsync(Supplier<? extends T> supplier, Executor executor) static <U> CompletableFuture<U> completedFuture(U value) static <U> CompletionStage<U> completedStage(U value) booleancompleteOnTimeout(T value, long timeout, TimeUnit unit) copy()A new future that completes as this one does.The executor the...Asyncmethods use when given none.exceptionally(Function<Throwable, ? extends T> fn) exceptionallyAsync(Function<Throwable, ? extends T> fn) exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor executor) exceptionallyCompose(Function<Throwable, ? extends CompletionStage<T>> fn) The exception of a future that failed.static <U> CompletableFuture<U> static <U> CompletionStage<U> failedStage(Throwable ex) get()Waits for the work to finish and returns its result.Waits up totimeoutfor the work to finish and returns its result.<U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) booleanWhetherFuture.cancel(boolean)cancelled the work before it ran.booleanbooleanisDone()Whether the work has finished: completed, failed or cancelled.join()The result, waiting for it; a failure is aCompletionException.A stage that completes as this one does, and that completing does not complete this one: acopy().<U> CompletableFuture<U> Fails this future with aTimeoutExceptionif it is not complete when the time has passed.The result of a future that completed normally.runAfterBoth(CompletionStage<?> other, Runnable action) runAfterBothAsync(CompletionStage<?> other, Runnable action) runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor) runAfterEither(CompletionStage<?> other, Runnable action) runAfterEitherAsync(CompletionStage<?> other, Runnable action) runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor) 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) thenAcceptAsync(Consumer<? super T> action) thenAcceptAsync(Consumer<? super T> action, Executor executor) <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor) <U> CompletableFuture<U> <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn) <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) <U,V> CompletableFuture <V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) <U,V> CompletableFuture <V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) <U,V> CompletableFuture <V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor) <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) thenRunAsync(Runnable action) thenRunAsync(Runnable action, Executor executor) toString()Returns a string representation of the object.whenComplete(BiConsumer<? super T, ? super Throwable> action) whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action) whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)
-
Constructor Details
-
CompletableFuture
public CompletableFuture()
-
-
Method Details
-
complete
-
completeExceptionally
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) Description copied from interface:FutureStops the work if it has not started. Answers whether it was cancelled; whether work already running is interrupted is up to the implementation and tomayInterruptIfRunning. -
isCancelled
public boolean isCancelled()Description copied from interface:FutureWhetherFuture.cancel(boolean)cancelled the work before it ran.- Specified by:
isCancelledin interfaceFuture<T>- Returns:
- true when cancelled
-
isDone
-
isCompletedExceptionally
public boolean isCompletedExceptionally() -
get
Description copied from interface:FutureWaits for the work to finish and returns its result.- Specified by:
getin interfaceFuture<T>- Returns:
- the result
- Throws:
InterruptedException- when the waiting thread is interruptedExecutionException- when the work threw;Throwable.getCause()is what it threw
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Description copied from interface:FutureWaits up totimeoutfor the work to finish and returns its result.- Specified by:
getin interfaceFuture<T>- Parameters:
timeout- how long to wait, inunitunit- the unit oftimeout- Returns:
- the result
- Throws:
InterruptedException- when the waiting thread is interruptedExecutionException- when the work threwTimeoutException- when it has not finished in time
-
join
The result, waiting for it; a failure is aCompletionException. -
getNow
-
resultNow
The result of a future that completed normally. -
exceptionNow
The exception of a future that failed. -
defaultExecutor
The executor the...Asyncmethods use when given none. -
newIncompleteFuture
-
completedFuture
-
failedFuture
-
completedStage
-
failedStage
-
supplyAsync
-
supplyAsync
-
runAsync
-
runAsync
-
completeAsync
-
completeAsync
-
allOf
-
anyOf
-
orTimeout
Fails this future with aTimeoutExceptionif it is not complete when the time has passed. -
completeOnTimeout
-
thenApply
- Specified by:
thenApplyin interfaceCompletionStage<T>
-
thenApplyAsync
- Specified by:
thenApplyAsyncin interfaceCompletionStage<T>
-
thenApplyAsync
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) - Specified by:
thenApplyAsyncin interfaceCompletionStage<T>
-
thenAccept
- Specified by:
thenAcceptin interfaceCompletionStage<T>
-
thenAcceptAsync
- Specified by:
thenAcceptAsyncin interfaceCompletionStage<T>
-
thenAcceptAsync
- Specified by:
thenAcceptAsyncin interfaceCompletionStage<T>
-
thenRun
- Specified by:
thenRunin interfaceCompletionStage<T>
-
thenRunAsync
- Specified by:
thenRunAsyncin interfaceCompletionStage<T>
-
thenRunAsync
- Specified by:
thenRunAsyncin interfaceCompletionStage<T>
-
thenCompose
- Specified by:
thenComposein interfaceCompletionStage<T>
-
thenComposeAsync
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) - Specified by:
thenComposeAsyncin interfaceCompletionStage<T>
-
thenComposeAsync
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) - Specified by:
thenComposeAsyncin interfaceCompletionStage<T>
-
handle
- Specified by:
handlein interfaceCompletionStage<T>
-
handleAsync
- Specified by:
handleAsyncin interfaceCompletionStage<T>
-
handleAsync
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) - Specified by:
handleAsyncin interfaceCompletionStage<T>
-
whenComplete
- Specified by:
whenCompletein interfaceCompletionStage<T>
-
whenCompleteAsync
- Specified by:
whenCompleteAsyncin interfaceCompletionStage<T>
-
whenCompleteAsync
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor) - Specified by:
whenCompleteAsyncin interfaceCompletionStage<T>
-
exceptionally
- Specified by:
exceptionallyin interfaceCompletionStage<T>
-
exceptionallyAsync
-
exceptionallyAsync
-
exceptionallyCompose
public CompletableFuture<T> exceptionallyCompose(Function<Throwable, ? extends CompletionStage<T>> fn) -
thenCombine
public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) - Specified by:
thenCombinein interfaceCompletionStage<T>
-
thenCombineAsync
public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) - Specified by:
thenCombineAsyncin interfaceCompletionStage<T>
-
thenCombineAsync
public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor) - Specified by:
thenCombineAsyncin interfaceCompletionStage<T>
-
thenAcceptBoth
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) - Specified by:
thenAcceptBothin interfaceCompletionStage<T>
-
thenAcceptBothAsync
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) - Specified by:
thenAcceptBothAsyncin interfaceCompletionStage<T>
-
thenAcceptBothAsync
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor) - Specified by:
thenAcceptBothAsyncin interfaceCompletionStage<T>
-
runAfterBoth
- Specified by:
runAfterBothin interfaceCompletionStage<T>
-
runAfterBothAsync
- Specified by:
runAfterBothAsyncin interfaceCompletionStage<T>
-
runAfterBothAsync
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor) - Specified by:
runAfterBothAsyncin interfaceCompletionStage<T>
-
applyToEither
public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn) - Specified by:
applyToEitherin interfaceCompletionStage<T>
-
applyToEitherAsync
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) - Specified by:
applyToEitherAsyncin interfaceCompletionStage<T>
-
applyToEitherAsync
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor) - Specified by:
applyToEitherAsyncin interfaceCompletionStage<T>
-
acceptEither
public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) - Specified by:
acceptEitherin interfaceCompletionStage<T>
-
acceptEitherAsync
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) - Specified by:
acceptEitherAsyncin interfaceCompletionStage<T>
-
acceptEitherAsync
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) - Specified by:
acceptEitherAsyncin interfaceCompletionStage<T>
-
runAfterEither
- Specified by:
runAfterEitherin interfaceCompletionStage<T>
-
runAfterEitherAsync
- Specified by:
runAfterEitherAsyncin interfaceCompletionStage<T>
-
runAfterEitherAsync
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor) - Specified by:
runAfterEitherAsyncin interfaceCompletionStage<T>
-
toCompletableFuture
- Specified by:
toCompletableFuturein interfaceCompletionStage<T>
-
copy
A new future that completes as this one does. -
minimalCompletionStage
A stage that completes as this one does, and that completing does not complete this one: a
copy().Not provided: the JDK's refusal. There the stage answered is of a class whose
complete,cancel,getand the rest throwUnsupportedOperationException, and whosetoCompletableFutureanswers yet another copy. Here it is an ordinary future, as whatcompletedStage(U)andfailedStage(Throwable)answer is, so code that casts the stage back to aCompletableFuture, or callstoCompletableFuture()on it, and completes that, completes the stage its dependents hang off instead of being refused. What the method is called for holds either way: whoever is handed the stage cannot complete, fail or cancel the future it was made from. -
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
-