Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public class CompletableFuture<T>
- Object
- CompletableFuture
ImplementsCompletionStage<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().
Constructors
public CompletableFuture() |
Methods
Inherited methods
Constructor details
CompletableFuture
public CompletableFuture()Method details
complete
public boolean complete(T value)completeExceptionally
public boolean completeExceptionally(Throwable ex)cancel
public boolean cancel(boolean mayInterruptIfRunning)mayInterruptIfRunning.Parameters
mayInterruptIfRunningboolean- whether a running task may be interrupted
Returns
isCancelled
public boolean isCancelled()cancel cancelled the work before it ran.Returns
isDone
public boolean isDone()Returns
isCompletedExceptionally
public boolean isCompletedExceptionally()get
public T get()
throws InterruptedException, ExecutionExceptionReturns
Throws
InterruptedException- when the waiting thread is interrupted
ExecutionException- when the work threw;
Throwable.getCauseis what it threw CancellationException- when the work was cancelled
get
public T get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutExceptiontimeout for the work to finish and returns its result.Parameters
timeoutlong- how long to wait, in
unit unitTimeUnit- the unit of
timeout
Returns
Throws
InterruptedException- when the waiting thread is interrupted
ExecutionException- when the work threw
TimeoutException- when it has not finished in time
CancellationException- when the work was cancelled
join
public T join()CompletionException.getNow
public T getNow(T valueIfAbsent)resultNow
public T resultNow()exceptionNow
public Throwable exceptionNow()defaultExecutor
public Executor defaultExecutor()...Async methods use when given none.newIncompleteFuture
public <U> CompletableFuture<U> newIncompleteFuture()completedFuture
public static <U> CompletableFuture<U> completedFuture(U value)failedFuture
public static <U> CompletableFuture<U> failedFuture(Throwable ex)completedStage
public static <U> CompletionStage<U> completedStage(U value)failedStage
public static <U> CompletionStage<U> failedStage(Throwable ex)supplyAsync
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)supplyAsync
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)runAsync
public static CompletableFuture<Void> runAsync(Runnable runnable)runAsync
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)completeAsync
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier)completeAsync
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier, Executor executor)allOf
public static CompletableFuture<Void> allOf(CompletableFuture<?>... given)anyOf
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... futures)orTimeout
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)TimeoutException if it is not complete
when the time has passed.completeOnTimeout
public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)thenApply
public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn)thenApplyAsync
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn)thenApplyAsync
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor)thenAccept
public CompletableFuture<Void> thenAccept(Consumer<? super T> action)thenAcceptAsync
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)thenAcceptAsync
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)thenRun
public CompletableFuture<Void> thenRun(Runnable action)thenRunAsync
public CompletableFuture<Void> thenRunAsync(Runnable action)thenRunAsync
public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)thenCompose
public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)thenComposeAsync
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn)thenComposeAsync
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor)handle
public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)handleAsync
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)handleAsync
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor)whenComplete
public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)whenCompleteAsync
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)whenCompleteAsync
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)exceptionally
public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)exceptionallyAsync
public CompletableFuture<T> exceptionallyAsync(Function<Throwable, ? extends T> fn)exceptionallyAsync
public CompletableFuture<T> exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor executor)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)thenCombineAsync
public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)thenCombineAsync
public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor)thenAcceptBoth
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)thenAcceptBothAsync
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)thenAcceptBothAsync
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor)runAfterBoth
public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)runAfterBothAsync
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action)runAfterBothAsync
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)applyToEither
public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)applyToEitherAsync
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn)applyToEitherAsync
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor)acceptEither
public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)acceptEitherAsync
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)acceptEitherAsync
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)runAfterEither
public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action)runAfterEitherAsync
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)runAfterEitherAsync
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor)toCompletableFuture
public CompletableFuture<T> toCompletableFuture()copy
public CompletableFuture<T> copy()minimalCompletionStage
public CompletionStage<T> 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, get and the rest throw
UnsupportedOperationException, and whose toCompletableFuture
answers yet another copy. Here it is an ordinary future, as what
completedStage and failedStage answer is, so code that casts
the stage back to a CompletableFuture, or calls
toCompletableFuture() 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
public String toString()