CompletableFuture and asynchronous composition
23 questions in 6 subtopics.
Composing stages
Which thread runs the callback incf.thenApply(f) compared with cf.thenApplyAsync(f) and cf.thenApplyAsync(f, executor)?Rewrite this blocking code as a non-blocking CompletableFuture chain using an executor ioPool:User u = userClient.get(id);
Account a = accountClient.get(u.accountId());
return render(u, a);
cf.thenApply(r -> repository.save(parse(r))), throughput collapses and the HTTP client reports timeouts. Explain.You are designing a shared asynchronous client library for internal services. Should its methods return CompletableFuture, CompletionStage or plain blocking results? Explain your API design, including executors and timeouts.ThenApply vs thenCompose
Why does this produceCompletableFuture<CompletableFuture<Order>>, and how do you fix it?CompletableFuture<User> user = findUser(id);
var order = user.thenApply(u -> findLatestOrder(u));
findLatestOrder returns CompletableFuture<Order>.In a code review, someone wrote cf.thenApply(x -> otherAsync(x).join()) to avoid nested futures. What is wrong with it?CompletableFuture has dozens of methods, most with Async variants and executor overloads. Why is the API so large, what does that say about API design, and would you expose it directly in your platform's public APIs for the next ten years?Combining futures
You need the results of three independent async calls,priceF, stockF and reviewsF, to build a ProductPage. Show how to combine them, and explain what allOf and anyOf return.What does cancel(true) do on a CompletableFuture, and how does that differ from a FutureTask returned by ExecutorService.submit?A page aggregates data from 20 services with CompletableFuture.allOf. When one service fails quickly, the user still waits for the slowest service before seeing an error. How would you make it fail fast, and does cancelling the others stop their work?Many pages in your product fan out to 10 to 30 services and combine futures in an aggregation layer that grows every year. What long-term alternatives would you consider, and how would you weigh their cost?Error handling in async chains
Compare exceptionally, handle andwhenComplete for error handling in a CompletableFuture chain. What exceptions do join() and get() throw?A service calls CompletableFuture.runAsync(() -> auditClient.send(event)) and never uses the returned future. Some audit events are missing and there are no error logs. Why?In cf.thenApply(this::parse).exceptionally(ex -> fallback()).thenApply(this::enrich), failures inside enrich are not handled and logs show the exception type CompletionException instead of the real cause. Explain both problems.Async code across your teams handles errors inconsistently: some chains swallow exceptions, others log the same failure at every stage, and alerts show CompletionException with no root cause. Define conventions for error handling in async chains and how you would roll them out.Timeouts
How doorTimeout and completeOnTimeout work (Java 9+), and do they stop the underlying work?An endpoint has a 2-second budget and calls three services in sequence, each wrapped in orTimeout(2, SECONDS). Users sometimes wait over 5 seconds. Design proper timeout handling.Requests pass through several services, each composing CompletableFutures with its own fixed timeouts. Users see both premature failures and long waits. Define a deadline and timeout convention for async calls across teams.CompletableFuture got timeouts only in Java 9 (orTimeout, completeOnTimeout), and cancelling it never stops the underlying work. What does this show about its design, how does structured concurrency address it, and what does that mean for multi-year bets?Choosing executors for async work
Which executor runssupplyAsync(supplier) when no executor is given, and why should you usually pass one for I/O work?After moving request processing into CompletableFuture chains on a custom executor, logs lose trace ids and security context is missing in some stages. Explain and fix.Your services mix CompletableFuture chains, Reactor pipelines and blocking code, and Java 25 is available. What standard for asynchronous composition would you set across teams for the next few years?Java went from Future (5) to CompletableFuture (8) to Flow and reactive libraries (9 and later) to virtual threads (21) and structured concurrency (preview). What does this sequence tell you about where to place long-term bets for asynchronous programming in your organisation?No question matches these choices.