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 zeroNotes
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
- Future.get() — Waits for a task to finish and returns its result.
- CompletableFuture.supplyAsync() — Runs a computation asynchronously and returns a `CompletableFuture`.
Related recipes
In the tutorial
- CompletableFuture — Asynchronous chains in Java — CompletableFuture.supplyAsync, thenApply, thenAccept, thenCombine, allOf, handling errors with exceptionally and join.