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>

  1. Object
  2. 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

public boolean complete(T value)
public boolean completeExceptionally(Throwable ex)
public boolean cancel(boolean mayInterruptIfRunning)Stops the work if it has not started.
public boolean isCancelled()Whether cancel cancelled the work before it ran.
public boolean isDone()Whether the work has finished: completed, failed or cancelled.
public boolean isCompletedExceptionally()
public T get() throws InterruptedException, ExecutionExceptionWaits for the work to finish and returns its result.
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutExceptionWaits up to timeout for the work to finish and returns its result.
public T join()The result, waiting for it; a failure is a CompletionException.
public T getNow(T valueIfAbsent)
public T resultNow()The result of a future that completed normally.
public Throwable exceptionNow()The exception of a future that failed.
public Executor defaultExecutor()The executor the ...Async methods use when given none.
public <U> CompletableFuture<U> newIncompleteFuture()
public static <U> CompletableFuture<U> completedFuture(U value)
public static <U> CompletableFuture<U> failedFuture(Throwable ex)
public static <U> CompletionStage<U> completedStage(U value)
public static <U> CompletionStage<U> failedStage(Throwable ex)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier)
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier, Executor executor)
public static CompletableFuture<Void> allOf(CompletableFuture<?>... given)
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... futures)
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)Fails this future with a TimeoutException if it is not complete when the time has passed.
public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)
public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor)
public CompletableFuture<Void> thenAccept(Consumer<? super T> action)
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
public CompletableFuture<Void> thenRun(Runnable action)
public CompletableFuture<Void> thenRunAsync(Runnable action)
public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn)
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor)
public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor)
public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)
public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)
public CompletableFuture<T> exceptionallyAsync(Function<Throwable, ? extends T> fn)
public CompletableFuture<T> exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor executor)
public CompletableFuture<T> exceptionallyCompose(Function<Throwable, ? extends CompletionStage<T>> fn)
public <U, V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)
public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn)
public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor)
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor)
public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action)
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)
public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn)
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor)
public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action)
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor)
public CompletableFuture<T> toCompletableFuture()
public CompletableFuture<T> copy()A new future that completes as this one does.
public CompletionStage<T> minimalCompletionStage()A stage that completes as this one does, and that completing does not complete this one: a copy.
public String toString()Returns a string representation of the object.

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)
Stops the work if it has not started. Answers whether it was cancelled; whether work already running is interrupted is up to the implementation and to mayInterruptIfRunning.

Parameters

mayInterruptIfRunning boolean
whether a running task may be interrupted

Returns

true when this call cancelled it

isCancelled

public boolean isCancelled()
Whether cancel cancelled the work before it ran.

Returns

true when cancelled

isDone

public boolean isDone()
Whether the work has finished: completed, failed or cancelled.

Returns

true once it will not change again

isCompletedExceptionally

public boolean isCompletedExceptionally()

get

public T get() throws InterruptedException, ExecutionException
Waits for the work to finish and returns its result.

Returns

the result

Throws

InterruptedException
when the waiting thread is interrupted
ExecutionException
when the work threw; Throwable.getCause is what it threw
CancellationException
when the work was cancelled

get

public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Waits up to timeout for the work to finish and returns its result.

Parameters

timeout long
how long to wait, in unit
unit TimeUnit
the unit of timeout

Returns

the result

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()
The result, waiting for it; a failure is a CompletionException.

getNow

public T getNow(T valueIfAbsent)

resultNow

public T resultNow()
The result of a future that completed normally.

exceptionNow

public Throwable exceptionNow()
The exception of a future that failed.

defaultExecutor

public Executor defaultExecutor()
The executor the ...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)
Fails this future with a 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()
A new future that completes as this one does.

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()
Returns 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())