Quick revision
The short answer
For two futures, thenCombine waits for both and passes both results to our function. For three or more, we use CompletableFuture.allOf(priceF, stockF, reviewsF).thenApply(v -> new ProductPage(priceF.join(), stockF.join(), reviewsF.join())). allOf returns CompletableFuture<Void>, so we read the results from the original futures, and join() does not block there because they are already done. anyOf returns CompletableFuture<Object> that completes with the first result or the first exception, and if any input fails, allOf completes exceptionally.
What to remember
In this answer, we started three independent calls in parallel and combined them. thenCombine waits for two futures and passes both values to a function. For three or more, allOf returns CompletableFuture<Void> that completes when all inputs complete, and we read the values from the original futures with join(), which does not block at that point. If any input fails, allOf completes exceptionally, so optional parts deserve their own fallbacks. anyOf returns CompletableFuture<Object> that completes with the first result or the first exception.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For two futures, thenCombine waits for both and passes both results to our function. For three or more, we use CompletableFuture.allOf(priceF, stockF, reviewsF).thenApply(v -> new ProductPage(priceF.join(), stockF.join(), reviewsF.join())). allOf returns CompletableFuture<Void>, so we read the results from the original futures, and join() does not block there because they are already done. anyOf returns CompletableFuture<Object> that completes with the first result or the first exception, and if any input fails, allOf completes exceptionally.
Why this question is asked
Product pages, dashboards and search results often need data from several services at once. Calling them one after another adds their latencies together; calling them in parallel and combining the results costs only the slowest one. Interviewers ask this to check that we can write the combination correctly and that we understand what allOf and anyOf actually return, which surprises many people. In this answer, we will start the three calls in parallel, combine two of them with thenCombine, combine all three with allOf, see what happens when one fails, and look at anyOf.
1Starting the Three Calls in Parallel
A CompletableFuture starts its work when it is created, not when we combine it. So the parallelism comes from starting all three calls before we wait for any of them. Each call goes to its service on its own, and the futures complete whenever their responses arrive.
Notice that we pass an executor. The blocking client calls should not run on the common pool, which is sized for CPU work. A virtual-thread executor is a simple choice on Java 21 and later.
2Combining Two Futures With thenCombine
thenCombine takes another future and a function of two arguments. It completes when both futures have completed, and then calls the function with both values.
This reads well for two inputs, and we could chain a second thenCombine for the third. With three or more inputs, though, nested thenCombine calls get hard to read, which is where allOf helps.
3Combining Three or More With allOf
allOf takes any number of futures and returns a future that completes when all of them have completed. The surprise is its type: CompletableFuture<Void>. It carries no results, because the inputs can have different types and there is no sensible way to return them together. So we read each result from its original future.
- Start priceF, stockF, reviewsFall three calls run at the same time
- allOf(...) waits for all three
CompletableFuture<Void>, no values inside - Did any input fail?YesallOf fails, thenApply is skippedNoRun thenApply
- Read values with join()no blocking: each future is complete
- Build ProductPageabout 200 ms in total, the slowest call
5 steps in order. Play them, or step through them one at a time.
Notice that join() inside the lambda does not block. The lambda runs only after allOf has completed, and allOf completes only after every input has completed, so each join() just reads a value that is already there. This is the one place where calling join() inside a stage is safe and idiomatic.
4What Happens When One Call Fails
If any input completes exceptionally, the future returned by allOf also completes exceptionally, with a CompletionException holding that failure as its cause. The thenApply stage is skipped, so the page is never built. Important detail: allOf still waits for all inputs to finish before it completes, even if one failed early. It does not fail fast.
Often only some parts of the page are essential. Reviews can be optional, so we can give that one future its own fallback before combining, for example reviewsF.exceptionally(ex -> List.of()). Then a failing review service gives a page without reviews instead of no page at all.
5What anyOf Returns
anyOf is the opposite: it completes as soon as any one input completes, with that input's result, or with its exception if the first one to finish failed. Its type is CompletableFuture<Object>, again because the inputs can have different types, so we usually need a cast.
anyOf fits racing equivalent sources, such as two replicas. But notice the catch: if the quickest one fails, anyOf fails, even if the other would have succeeded a moment later. And the slower future keeps running, because anyOf does not cancel it.
6Follow-up questions
Why does allOf return CompletableFuture<Void> instead of a list of results?
Because its inputs can have different result types, here Price, Stock and List<Review>, and Java has no type for a list of mixed types other than Object. If all inputs have the same type, we can collect them ourselves with allOf(...).thenApply(v -> futures.stream().map(CompletableFuture::join).toList()).
Does allOf start the futures?
No. The futures started when they were created by supplyAsync or by the client. allOf only watches them. If we created them lazily inside the lambda, the calls would run one after another.
Is there a fail-fast version of allOf?
Not in the JDK. We can build one by completing a separate result future exceptionally from each input's exceptionally handler, or use structured concurrency, which is still a preview feature in Java 25, so check its status in your JDK.
7Conclusion
In this answer, we started three independent calls in parallel and combined them. thenCombine waits for two futures and passes both values to a function. For three or more, allOf returns CompletableFuture<Void> that completes when all inputs complete, and we read the values from the original futures with join(), which does not block at that point. If any input fails, allOf completes exceptionally, so optional parts deserve their own fallbacks. anyOf returns CompletableFuture<Object> that completes with the first result or the first exception.
Open with
First I start all three calls so they run at the same time, for example with supplyAsync on a virtual-thread executor rather than the common pool. Then I combine them.
Then, if they want more
For two futures, thenCombine is the neat option: priceF.thenCombine(stockF, (p, s) -> ...) waits for both and gives me both values. For three or more, I use allOf(priceF, stockF, reviewsF).thenApply(v -> new ProductPage(priceF.join(), stockF.join(), reviewsF.join())).
The thing to know is that allOf returns a CompletableFuture of Void, because the inputs have different types, so it carries no results and I read them from the original futures. Calling join inside that lambda does not block, since the lambda runs only after every input has completed.
If any input fails, allOf completes exceptionally with a CompletionException holding the cause, and it still waits for the others to finish first, so it is not fail-fast.
For parts of the page that are optional, like reviews, I would add an exceptionally fallback to that future before combining. anyOf is different: it returns a CompletableFuture of Object that completes with whichever input finishes first, result or exception, and it does not cancel the rest.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You need the results of three independent async calls, priceF, stockF and reviewsF, to...": https://scrivio-interview-library.netlify.app/java/completable-future/cd127ebb23/