Bird
0
0

Identify the error in this Spring Boot async method:

medium📝 Debug Q14 of 15
Spring Boot - Async Processing
Identify the error in this Spring Boot async method:
@Async
public void process() {
    CompletableFuture result = CompletableFuture.supplyAsync(() -> {
        throw new RuntimeException("Fail");
    });
    System.out.println(result.get());
}
ACalling <code>result.get()</code> blocks the async thread, defeating async benefits.
BThrowing RuntimeException inside supplyAsync is not allowed.
CThe method must return CompletableFuture<String>, not void.
DMissing @EnableAsync annotation on the class.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze use of result.get()

    Calling get() waits for the CompletableFuture to finish, blocking the async thread.
  2. Step 2: Understand async best practices

    Blocking defeats async purpose; exceptions inside supplyAsync are allowed and handled asynchronously.
  3. Final Answer:

    Calling result.get() blocks the async thread, defeating async benefits. -> Option A
  4. Quick Check:

    Blocking call inside async method = C [OK]
Quick Trick: Avoid blocking calls like get() inside async methods [OK]
Common Mistakes:
  • Thinking exceptions inside supplyAsync are disallowed
  • Believing async methods must always return CompletableFuture
  • Forgetting to enable async globally (not always required)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes