Section 19 Β· 28 questions

CompletableFuture and Asynchrony

28 interview questions and answers in the CompletableFuture and Asynchrony section.

English CompletableFuture and Asynchrony Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. What is CompletableFuture and how does it differ from Future
  2. What are the main advantages of CompletableFuture over Future
  3. How to create a CompletableFuture that is already completed with result
  4. What is the difference between thenApply() and thenCompose()
  5. What do thenAccept() and thenRun() methods do
  6. How to handle exceptions in CompletableFuture chain
  7. What is the difference between handle(), exceptionally() and whenComplete()
  8. How to combine results of multiple CompletableFutures
  9. What does allOf() method do and when to use it
  10. What does anyOf() method do and when is it useful
  11. What is the difference between thenApply() and thenApplyAsync()
  12. What thread pool is used by default for async methods?
  13. How to specify your own Executor for CompletableFuture
  14. What is blocking code and how to distinguish it from non-blocking
  15. Why is it important to avoid blocking operations in CompletableFuture
  16. How to properly execute multiple parallel requests to microservices
  17. What does supplyAsync() method do and when to use it
  18. How to cancel CompletableFuture execution
  19. What happens if an exception occurs in CompletableFuture chain
  20. Can you reuse a single CompletableFuture in multiple chains
  21. How to implement timeout for CompletableFuture
  22. What does orTimeout() method do in Java 9+
  23. What is the difference between thenCombine() and thenCompose()
  24. How to test code with CompletableFuture
  25. When is it better to use CompletableFuture vs reactive programming
  26. What does join() method do and how does it differ from get()
  27. Can you manually complete a CompletableFuture with a result
  28. How to Implement Retry Logic with CompletableFuture

Study navigator

28 interview preparation questions for Middle/Senior Java Developer.


πŸ“‹ All questions

# Question Difficulty
1 What is CompletableFuture and how does it differ from Future ⭐⭐
2 What are the main advantages of CompletableFuture over Future ⭐⭐
3 How to create a CompletableFuture that is already completed with result ⭐
4 What is the difference between thenApply() and thenCompose() ⭐⭐
5 What do thenAccept() and thenRun() methods do ⭐
6 How to handle exceptions in CompletableFuture chain ⭐⭐
7 What is the difference between handle(), exceptionally() and whenComplete() ⭐⭐
8 How to combine results of multiple CompletableFutures ⭐⭐
9 What does allOf() method do and when to use it ⭐⭐
10 What does anyOf() method do and when is it useful ⭐⭐
11 What is the difference between thenApply() and thenApplyAsync() ⭐⭐
12 What thread pool is used by default for async methods ⭐⭐⭐
13 How to specify your own Executor for CompletableFuture ⭐⭐⭐
14 What is blocking code and how to distinguish it from non-blocking ⭐⭐
15 Why is it important to avoid blocking operations ⭐⭐⭐
16 How to properly execute multiple parallel requests ⭐⭐⭐
17 What does supplyAsync() method do and when to use it ⭐⭐
18 How to cancel CompletableFuture execution ⭐⭐
19 What happens if an exception occurs ⭐⭐
20 Can a CompletableFuture be reused ⭐⭐⭐
21 How to implement timeout for CompletableFuture ⭐⭐
22 What does orTimeout() method do in Java 9+ ⭐⭐
23 What is the difference between thenCombine() and thenCompose() ⭐⭐
24 How to test code with CompletableFuture ⭐⭐
25 CompletableFuture vs reactive programming ⭐⭐⭐
26 What does join() method do and how does it differ from get() ⭐⭐
27 Can you manually complete a CompletableFuture with a result ⭐⭐⭐
28 How to implement retry logic with CompletableFuture ⭐⭐⭐

πŸ—ΊοΈ 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    β”‚        β”‚                β”‚        β”‚                    β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 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