java.util.concurrent · java.base · since Java 1.8
CompletableFuture
public class CompletableFuture<T>
implements Future<T>, CompletionStage<T>A composable promise: chain (thenApply/thenCompose), combine (thenCombine/allOf), recover (exceptionally), and time-bound (orTimeout) asynchronous work.
Key methods
static CompletableFuture<T> supplyAsync(Supplier<T>, Executor) | Start async work — ALWAYS pass your executor for blocking tasks. |
CompletableFuture<U> thenApply(Function<T,U>) | Map the result. |
CompletableFuture<U> thenCompose(Function<T, CompletableFuture<U>>) | FlatMap — chain dependent async calls. |
CompletableFuture<V> thenCombine(other, BiFunction) | Zip two independent futures. |
static CompletableFuture<Void> allOf/anyOf(CompletableFuture<?>...) | Fan-in. |
CompletableFuture<T> exceptionally(Function<Throwable,T>) | Fallback on failure. |
CompletableFuture<T> orTimeout(long, TimeUnit) | Fail with TimeoutException after the deadline (Java 9). |
T join() | get() without checked exceptions (throws CompletionException). |