Bird
0
0

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

medium📝 component behavior Q4 of 15
Spring Boot - Async Processing
Given the following code snippet, 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 method started");
  }

  public void mainMethod() {
    asyncMethod();
    System.out.println("Main method finished");
  }
}
AMain method finished only
BMain method finished\nAsync method started
CAsync method started\nMain method finished
DAsync method started only
Step-by-Step Solution
Solution:
  1. Step 1: Identify the method call context

    asyncMethod() is called directly from mainMethod() within the same class, bypassing the Spring AOP proxy.
  2. Step 2: Trace output order

    Internal calls to @Async methods run synchronously, so "Async method started" prints first, followed by "Main method finished".
  3. Final Answer:

    Async method started\nMain method finished -> Option C
  4. Quick Check:

    Internal @Async call = synchronous execution [OK]
Quick Trick: Direct calls to @Async methods within the same class run synchronously [OK]
Common Mistakes:
  • Expecting asynchronous execution on internal calls
  • Thinking main method finishes before async print
  • Ignoring Spring proxy self-invocation bypass

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes