CompletableFuture and Asynchrony
28 interview questions and answers in the CompletableFuture and Asynchrony section.
Questions in this section
- What is CompletableFuture and how does it differ from Future
- What are the main advantages of CompletableFuture over Future
- How to create a CompletableFuture that is already completed with result
- What is the difference between thenApply() and thenCompose()
- What do thenAccept() and thenRun() methods do
- How to handle exceptions in CompletableFuture chain
- What is the difference between handle(), exceptionally() and whenComplete()
- How to combine results of multiple CompletableFutures
- What does allOf() method do and when to use it
- What does anyOf() method do and when is it useful
- What is the difference between thenApply() and thenApplyAsync()
- What thread pool is used by default for async methods?
- How to specify your own Executor for CompletableFuture
- What is blocking code and how to distinguish it from non-blocking
- Why is it important to avoid blocking operations in CompletableFuture
- How to properly execute multiple parallel requests to microservices
- What does supplyAsync() method do and when to use it
- How to cancel CompletableFuture execution
- What happens if an exception occurs in CompletableFuture chain
- Can you reuse a single CompletableFuture in multiple chains
- How to implement timeout for CompletableFuture
- What does orTimeout() method do in Java 9+
- What is the difference between thenCombine() and thenCompose()
- How to test code with CompletableFuture
- When is it better to use CompletableFuture vs reactive programming
- What does join() method do and how does it differ from get()
- Can you manually complete a CompletableFuture with a result
- How to Implement Retry Logic with CompletableFuture
Study navigator
28 interview preparation questions for Middle/Senior Java Developer.
π All questions
πΊοΈ Topic dependency map
ββββββββββββββββββββββββββββββββββββββββββββ
β BASICS (1-5) β
β 1. CompletableFuture vs Future β
β 2. Advantages β
β 3. completedFuture β
β 4. thenApply vs thenCompose β
β 5. thenAccept vs thenRun β
ββββββββββββββββββββ¬ββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β EXCEPTIONS β β COMBINING β β EXECUTORS β
β (6-7, 19) β β (8-11, 23) β β (12-13, 17) β
β 6. Handling β β 8. Combining β β 12. Common Pool β
β 7. handle/ β β 9. allOf β β 13. Custom Exec. β
β exceptionallyβ β 10. anyOf β β 17. supplyAsync β
β 19. Exception β β 11. *Async β β β
β in chain β β 23. thenCombineβ β β
βββββββββ¬ββββββββ βββββββββ¬ββββββββ ββββββββββ¬ββββββββββββ
β β β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β BLOCKING AND TIMEOUT (14-16, 20-22) β
β 14. Blocking vs Non-blocking β
β 15. Why avoid blocking β
β 16. Parallel requests β
β 20. Timeout β
β 21. orTimeout (Java 9+) β
ββββββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β ADVANCED β β TESTING β β PATTERNS β
β (18, 25-28) β β (24) β β (27) β
β 18. Cancel β β 24. Testing β β 27. Retry β
β 25. join vs getβ β β β β
β 26. obtrude β β β β β
β 25. CF vs β β β β β
β Reactive β β β β β
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
π― Recommended study order
π’ Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Basics | Q1, Q2, Q3 | How it differs from Future, completedFuture |
| 2 | Chains | Q4, Q5 | thenApply, thenCompose, thenAccept, thenRun |
| 3 | Exceptions | Q6, Q7 | exceptionally, handle, whenComplete |
| 4 | join vs get | Q25 | How they differ, why join is preferable |
π‘ Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Combining | Q8, Q9, Q10, Q23 | allOf, anyOf, thenCombine, thenCompose |
| 2 | Async methods | Q11, Q17 | thenApply vs thenApplyAsync, supplyAsync |
| 3 | Blocking | Q14, Q15 | What is blocking, why dangerous in commonPool |
| 4 | Timeout | Q20, Q21 | Manual timeout, orTimeout (Java 9+) |
| 5 | Testing | Q24 | Awaitility, Testcontainers, Virtual Threads |
π΄ Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Executors | Q12, Q13 | CommonPool internals, custom executors, ManagedBlocker |
| 2 | Parallel requests | Q16 | Semaphore + dedicated executor, error handling |
| 3 | Cancel & obtrude | Q18, Q26 | cancel() mechanics, obtrudeValue dangers |
| 4 | Reuse CF | Q19 | Fan-out, memory leak via reference chains |
| 5 | CF vs Reactive | Q25 | When to use each, Virtual Threads impact |
| 6 | Retry patterns | Q28 | exceptionallyCompose, exponential backoff + jitter |
π Key topic connections
Topic: Basics
Q1 (CF vs Future) β Q2 (Advantages) β Q3 (completedFuture)
Topic: Chains
Q4 (thenApply vs thenCompose) β Q5 (thenAccept vs thenRun) β Q23 (thenCombine vs thenCompose)
Key connections:
- Q4 β Q23: thenCompose = sequential, thenCombine = parallel
- Q5 β Q4: thenAccept = Consumer (no result), thenRun = Runnable (no input)
Topic: Exceptions
Q6 (Handling) β Q7 (handle/exceptionally/whenComplete) β Q19 (Exception in chain)
Topic: Combining
Q8 (Combining) β Q9 (allOf) β Q10 (anyOf)
Topic: Executors and Blocking
Q12 (Common Pool) β Q13 (Custom Executor) β Q14 (Blocking) β Q15 (Why avoid)
β
Q17 (supplyAsync) β Q16 (Parallel requests)
Key connections:
- Q12 β Q15: Blocking in commonPool β thread pool starvation
- Q13 β Q16: Parallel requests β dedicated executor, NOT commonPool
- Q14 β Q15: Blocking vs non-blocking β why blocking is dangerous
Topic: Timeout and Cancel
Q20 (Timeout) β Q21 (orTimeout)
Q18 (Cancel) β Q26 (obtrudeValue)
π― Cheat sheet: what to know for each level
π’ Junior
- CompletableFuture = async result + callback chains
- thenApply = transformation, thenAccept = side effect, thenRun = action without data
- exceptionally = fallback on error, handle = handles both success and error
- join() = blocking without checked exceptions, get() = blocking with checked exceptions
π‘ Middle
- supplyAsync by default β ForkJoinPool.commonPool()
- Blocking in commonPool β thread pool starvation for all tasks
- allOf = wait for ALL, anyOf = wait for FIRST
- orTimeout (Java 9+) = built-in timeout, in Java 8 β manually via ScheduledExecutor
- exceptionallyCompose (Java 12+) = non-blocking retry
π΄ Senior
- commonPool size = availableProcessors - 1, changeable via System.setProperty
- ManagedBlocker notifies ForkJoinPool about blocking operation
- obtrudeValue violates βsingle completionβ contract β race conditions
- CF vs Reactive: CF = single result, Reactive = streams + backpressure
- Virtual Threads (Java 21+) simplify blocking code β CF less critical
- Retry: exponential backoff + jitter prevents thundering herd
π Format of each file
Each file contains:
- π’ Junior Level β basic understanding, simple analogies, examples
- π‘ Middle Level β internals, typical mistakes, practical examples
- π΄ Senior Level β deep dive, edge cases, production experience, monitoring
- π― Interview cheat sheet β key points, frequent questions, red flags, related topics