The 30 second answer
A Future only lets you check if the work is done, cancel it, or block on get to wait for the result. CompletableFuture, added in Java 8, lets me chain the next steps without blocking: transform the result, call another async step, combine two results, or wait for many. It also handles errors in the chain. For blocking I/O, I pass my own executor instead of using the common pool.
Future<T> (Java 5) offers isDone(), cancel() and a blocking get(). You can't attach a callback or compose two futures.
CompletableFuture<T> (Java 8) implements both Future and CompletionStage:
thenApplytransforms the result;thenComposechains another async call (likeflatMap).thenCombinemerges two independent results;allOf/anyOfwait for several.exceptionally,handleandwhenCompletedeal with failures.complete(value)lets you finish it by hand.
CompletableFuture<List<Order>> orders =
CompletableFuture.supplyAsync(() -> loadUser(id), ioPool)
.thenCompose(u -> CompletableFuture.supplyAsync(() -> loadOrders(u), ioPool))
.orTimeout(2, TimeUnit.SECONDS) // Java 9
.exceptionally(ex -> List.of());Async methods without an executor argument use ForkJoinPool.commonPool(). [Opinion] For blocking I/O pass a dedicated executor so you don't starve the common pool, which parallel streams also share. On Java 21, a virtual-thread-per-task executor is a good fit.
Alternate interview scenarios
"An application uses CompletableFuture heavily, and response times grow under load. How do you find the concurrency bottleneck?"
The usual cause is a starved executor, not CompletableFuture itself. The Javadoc says async methods without an explicit executor use the common ForkJoinPool. That pool is sized to the CPU count and shared with parallel streams, so blocking I/O on it lets a few slow calls occupy every worker. Other causes are a join() or get() inside a stage, which blocks a pool thread while it waits for another stage on the same pool, and a bounded custom pool whose queue grows. To find it, I take a thread dump, see which pool's threads are busy and what they block on, and look at queue length and task wait time for each executor. The fix is a dedicated, bounded executor for each kind of blocking work, or virtual threads, plus orTimeout on every remote call.
"ExecutorService and CompletableFuture: how does each one report an exception thrown inside a task?"
It depends on how the task was started. With ExecutorService.submit, the exception is stored in the Future, and get() throws an ExecutionException whose cause is the original exception. If nobody ever calls get(), the error is silently lost, which is a common production bug. With execute, there is no Future: the exception kills the worker thread and goes to the thread's uncaught exception handler, which by default just prints the stack trace. With CompletableFuture, the exception travels down the chain: dependent stages are skipped, and exceptionally, handle or whenComplete can recover or log it. join() throws an unchecked CompletionException and get() throws a checked ExecutionException, both wrapping the cause. I verified on Java 21 that even exceptionally on a failed supplyAsync receives a CompletionException, so I unwrap with getCause() before checking the type. My rule is to always attach a handler or call get() and never fire and forget with submit.
Sources (2)
- Future Javadocdocs.oracle.com
- CompletableFuture Javadocdocs.oracle.com