Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

CompletableFuture.join()

Waits for the result of a `CompletableFuture`; errors arrive as unchecked exceptions.

Available since: Java 8

Syntax

public T join()

Return value

TThe result of the computation.

Examples

A task error on join():

void main() {
    var failing = CompletableFuture.supplyAsync(() -> 10 / 0);
    try {
        failing.join();
    } catch (CompletionException e) {
        IO.println(e.getCause());
    }
}

Output:

java.lang.ArithmeticException: / by zero

Notes

It does the same as get() but needs no checked exception handling: a task error arrives wrapped in CompletionException. That makes join() handy in lambdas and streams. The call blocks the current thread — inside a chain prefer continuing with thenApply().

See also

Related recipes

In the tutorial

  • CompletableFuture — Asynchronous chains in Java — CompletableFuture.supplyAsync, thenApply, thenAccept, thenCombine, allOf, handling errors with exceptionally and join.

Thanks for reading!

Donate to the site — we work hard for you.