Bird
0
0

Given the code below, what will be the output when mainMethod() is called?

medium📝 component behavior Q13 of 15
Spring Boot - Async Processing
Given the code below, what will be the output when mainMethod() is called?
@EnableAsync
@Configuration
public class AppConfig {}

@Service
public class MyService {
  @Async
  public void asyncMethod() {
    System.out.println("Async task started");
  }

  public void mainMethod() {
    System.out.println("Main method start");
    asyncMethod();
    System.out.println("Main method end");
  }
}
AMain method start Main method end Async task started
BMain method start Async task started Main method end
CAsync task started Main method start Main method end
DMain method start Main method end
Step-by-Step Solution
Solution:
  1. Step 1: Understand async method behavior

    The @Async method runs asynchronously only when called from a different Spring-managed bean proxy. Calling asyncMethod() directly within the same class does NOT run asynchronously, so it executes synchronously.
  2. Step 2: Trace output order

    All print statements execute in order: "Main method start", "Async task started", then "Main method end".
  3. Final Answer:

    Main method start Async task started Main method end -> Option B
  4. Quick Check:

    Direct self-invocation bypasses async proxy, so method runs synchronously [OK]
Quick Trick: Async methods called within same class run synchronously [OK]
Common Mistakes:
  • Assuming async prints immediately in order
  • Thinking async blocks main thread
  • Ignoring that async runs in background thread only when called via proxy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes