0
0
Spring Bootframework~10 mins

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

Choose your learning style9 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.