Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
Why should you use CompletableFuture instead of Future?
A plain Future only lets you check whether a task is done (isDone()) or block until it finishes (get()). There's no way to attach a callback, react to completion asynchronously, or combine it with another Future.
CompletableFuture, added in Java 8, implements both Future and CompletionStage, adding non-blocking composition.
CompletableFuture.supplyAsync(() -> fetchUser()) .thenApply(User::getName) .thenCombine(fetchPrefsAsync(), (name, prefs) -> render(name, prefs)) .exceptionally(ex -> fallbackView()) .thenAccept(view -> display(view));
Methods like thenApply, thenCompose, and thenCombine let you chain dependent work without blocking a thread waiting on get(), exceptionally/handle give you structured error handling in the chain, and it can also be completed manually with complete(), which a plain Future cannot be.
In short, Future is a passive handle to a result; CompletableFuture is an active, composable pipeline for asynchronous work.
More Related questions...