Bird
Raised Fist0
Spring Bootframework~10 mins

@EnableAsync annotation in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - @EnableAsync annotation
Start Application
↓
Spring Boot Loads Context
↓
Detect @EnableAsync
↓
Create Async Executor
↓
Scan for @Async Methods
↓
Run @Async Methods in Separate Threads
↓
Main Thread Continues Without Waiting
↓
Async Task Completes Independently
This flow shows how Spring Boot detects @EnableAsync, sets up async support, and runs @Async methods on separate threads so the main thread doesn't wait.
Execution Sample
Spring Boot
@SpringBootApplication
@EnableAsync
public class App {
  @Async
  public void runTask() {
    // long task
  }
}
This code enables async support and runs runTask() asynchronously on a different thread.
Execution Table
StepActionThreadState BeforeState AfterOutput/Effect
1Start ApplicationmainNo contextSpring context createdApplication starts
2Detect @EnableAsyncmainNo async supportAsync support enabledAsync executor created
3Call runTask()mainNo task runningTask submitted to executorMain thread continues
4Execute runTask()async-thread-1Task queuedTask runningLong task runs separately
5runTask() completesasync-thread-1Task runningTask doneAsync task finished
6Main thread continuesmainTask submittedNo changeMain thread not blocked
7Application runs normallymainAsync task doneNo changeApp responsive
💡 Async task completes independently; main thread never waits for it.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Spring ContextNot createdCreatedCreatedCreatedCreated
Async SupportDisabledEnabledEnabledEnabledEnabled
runTask StateNot calledSubmittedRunningCompletedCompleted
Main Thread StateIdleContinuesContinuesContinuesContinues
Key Moments - 3 Insights
Why does the main thread not wait for the @Async method to finish?
Because @EnableAsync sets up a separate thread pool. The main thread submits the task and continues immediately, as shown in execution_table step 3 and 6.
What happens if @EnableAsync is missing but @Async is used?
The @Async annotation will be ignored and methods run synchronously on the main thread, so no separate thread is created. This is why detecting @EnableAsync is crucial (step 2).
How does Spring know which methods to run asynchronously?
Spring scans for methods annotated with @Async after enabling async support (step 4). Only those methods run on separate threads.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the async task start running on a separate thread?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Check the 'Thread' and 'State After' columns in step 4.
According to the variable tracker, what is the state of 'runTask State' after step 5?
ASubmitted
BCompleted
CRunning
DNot called
💡 Hint
Look at the 'runTask State' row under 'After Step 5' in variable_tracker.
If @EnableAsync was removed, how would the main thread behave when calling runTask()?
AIt would run runTask() asynchronously anyway
BIt would throw an error
CIt would run runTask() synchronously and wait
DIt would skip runTask()
💡 Hint
Refer to key_moments about missing @EnableAsync and its effect.
Concept Snapshot
@EnableAsync enables Spring Boot to run methods annotated with @Async on separate threads.
Add @EnableAsync on a configuration class to activate async support.
Use @Async on methods to run them asynchronously.
Main thread submits async tasks and continues without waiting.
Spring manages thread pool and task execution automatically.
Full Transcript
When you add @EnableAsync to your Spring Boot application, it tells Spring to look for methods marked with @Async and run them on separate threads. This means your main program doesn't have to wait for these methods to finish. The flow starts when the application launches and Spring creates the context. It detects @EnableAsync and sets up an async executor. When you call an @Async method, the main thread submits the task to this executor and keeps running. Meanwhile, the async method runs on a different thread and finishes independently. If you forget @EnableAsync, your @Async methods will run like normal methods on the main thread, blocking it. This setup helps keep your app responsive by doing long tasks in the background.

Practice

(1/5)
1. What is the main purpose of the @EnableAsync annotation in a Spring Boot application?
easy
A. To enable automatic database transactions
B. To enable REST API endpoint scanning
C. To enable running methods asynchronously in the background
D. To enable security features like authentication

Solution

  1. Step 1: Understand what @EnableAsync does

    This annotation activates Spring's ability to run methods asynchronously, meaning in the background without blocking the main thread.
  2. Step 2: Compare with other options

    Other options relate to transactions, REST scanning, or security, which are unrelated to @EnableAsync.
  3. Final Answer:

    To enable running methods asynchronously in the background -> Option C
  4. Quick Check:

    @EnableAsync = run methods asynchronously [OK]
Hint: Remember: Enable async means run tasks in background [OK]
Common Mistakes:
  • Confusing @EnableAsync with transaction management
  • Thinking it enables REST endpoints
  • Assuming it controls security features
2. Which of the following is the correct way to enable asynchronous method execution in a Spring Boot application?
easy
A. Add @Async on configuration class and @EnableAsync on methods
B. Add @Async on the main application class only
C. Add @EnableAsync on every method you want async
D. Add @EnableAsync on a configuration class and @Async on methods

Solution

  1. Step 1: Identify correct annotation placement

    @EnableAsync must be on a configuration or main class to activate async support globally.
  2. Step 2: Mark methods with @Async

    Methods that should run asynchronously need the @Async annotation.
  3. Final Answer:

    Add @EnableAsync on a configuration class and @Async on methods -> Option D
  4. Quick Check:

    Enable async globally + mark methods async = Add @EnableAsync on a configuration class and @Async on methods [OK]
Hint: Enable async on config class, async on methods [OK]
Common Mistakes:
  • Putting @Async on the main class only
  • Using @EnableAsync on methods instead of config
  • Mixing up @Async and @EnableAsync roles
3. 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");
  }
}
medium
A. Main method start Main method end Async task started
B. Main method start Async task started Main method end
C. Async task started Main method start Main method end
D. Main method start Main method end

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]
Hint: 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
4. Consider this Spring Boot setup:
@Configuration
public class AsyncConfig {
  @Async
  public void runAsync() {
    System.out.println("Running async");
  }
}

What is the main problem with this code?
medium
A. Missing @EnableAsync annotation to activate async support
B. Methods annotated with @Async must return a value
C. @Async cannot be used inside @Configuration classes
D. The method must be static to run asynchronously

Solution

  1. Step 1: Check if async support is enabled

    Without @EnableAsync on a configuration or main class, Spring will not run @Async methods asynchronously.
  2. Step 2: Validate other options

    @Async methods can be void, can be in any Spring bean, and do not need to be static.
  3. Final Answer:

    Missing @EnableAsync annotation to activate async support -> Option A
  4. Quick Check:

    Enable async missing = async won't run [OK]
Hint: Always add @EnableAsync to activate async methods [OK]
Common Mistakes:
  • Thinking @Async needs return value
  • Believing @Async can't be in config classes
  • Assuming method must be static
5. You want to run multiple slow tasks in parallel in a Spring Boot app. Which setup correctly uses @EnableAsync and @Async to achieve this?
@Configuration
@EnableAsync
public class AsyncConfig {}

@Service
public class TaskService {
  @Async
  public CompletableFuture<String> slowTask(String name) {
    Thread.sleep(2000);
    return CompletableFuture.completedFuture(name + " done");
  }
}

// In some controller or service
public void runTasks() {
  CompletableFuture<String> t1 = taskService.slowTask("Task1");
  CompletableFuture<String> t2 = taskService.slowTask("Task2");
  CompletableFuture.allOf(t1, t2).join();
  System.out.println("All tasks completed");
}
hard
A. This setup runs slowTask methods in parallel without blocking main thread
B. This setup runs slowTask methods sequentially blocking main thread
C. Missing @Async on slowTask method, so runs sequentially
D. Missing @EnableAsync, so async calls run synchronously

Solution

  1. Step 1: Check annotations and method return type

    @EnableAsync is present to enable async support, and @Async marks slowTask to run asynchronously returning CompletableFuture.
  2. Step 2: Understand method calls and joining

    Calling slowTask twice starts both tasks in parallel threads. CompletableFuture.allOf(...).join() waits for both to finish without blocking main thread execution during tasks.
  3. Final Answer:

    This setup runs slowTask methods in parallel without blocking main thread -> Option A
  4. Quick Check:

    EnableAsync + Async + CompletableFuture = parallel tasks [OK]
Hint: Use @EnableAsync + @Async + CompletableFuture for parallel tasks [OK]
Common Mistakes:
  • Forgetting @EnableAsync disables async behavior
  • Not returning CompletableFuture causes sync execution
  • Calling async methods without waiting causes premature exit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard