Concurrency basics · interview question
In which scenario would you use.thenCompose() over.thenApply() in CompletableFuture?
Multiple choiceMediumchapter-15-curatedpremiumconcurrencycompletablefuture
Answer and explanation
Correct answer
A. When you want to return another CompletableFuture.
Why
.thenCompose() is used when a function returns another CompletableFuture, allowing you to flatten nested futures into a single asynchronous computation, unlike.thenApply(), which transforms the result without flattening.
Test yourself on Concurrency basics
A free test with instant, explained results. No account needed.
More Concurrency basics questions
- How can you ensure that a CompletableFuture runs on a specific Executor?
- Which situation benefits most from using AtomicInteger over synchronized blocks?
- Which CompletableFuture method should be used to execute a task asynchronously in a non-blocking manner?
- Which CompletableFuture method should be used to handle both results and exceptions in a unified way?
- How does AtomicInteger's getAndIncrement method differ from incrementAndGet?
- How does AtomicReference help in implementing non-blocking algorithms?