CompletableFuture and async composition question 92 of 180 junior
What does CompletableFuture add over Future?
- Nothing, it is a renamed
Future - Correct: Composition and callbacks: you can chain, combine and handle errors without blocking, and complete it from outside
- It runs tasks faster
- It does not need an executor
Future gives you get() and isDone(), which means the only way to use a result is to block on it
or poll for it. Composing two of them means blocking on the first to start the second.
CompletableFuture is a promise you can attach continuations to: thenApply, thenCompose,
thenCombine, allOf, exceptionally, handle. Nothing blocks, and the chain runs when the value
arrives. It is also completable from the outside, via complete(value) or
completeExceptionally(e), which is what makes it the natural bridge from a callback-based client
library.
Quiz yourself on CompletableFuture and async composition Suggest a correction