0
0
Spring Bootframework~10 mins

@Async for async methods in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Async for async methods
Call method annotated with @Async
Spring creates a new thread
Method runs in background
Main thread continues immediately
Optional: Future result returned
Caller can check or wait for result later
When a method is marked with @Async, Spring runs it in a new thread so the main thread does not wait and continues immediately.
Execution Sample
Spring Boot
@Async
public CompletableFuture<String> asyncMethod() throws InterruptedException {
    Thread.sleep(1000);
    return CompletableFuture.completedFuture("Done");
}
This method runs asynchronously, sleeping 1 second in a background thread, then returns "Done" wrapped in a CompletableFuture.
Execution Table
StepActionThreadState BeforeState AfterOutput/Effect
1Caller invokes asyncMethod()Main ThreadNo async task runningAsync task startedMain thread continues without waiting
2Spring creates new thread for asyncMethodNew ThreadMethod not startedMethod runningBackground thread sleeps 1 second
3asyncMethod completes after sleepNew ThreadSleepingCompletedReturns CompletableFuture with "Done"
4Caller optionally checks CompletableFutureMain ThreadFuture incompleteFuture completedCaller can get result "Done"
5Execution endsBoth ThreadsMethod doneIdleMain thread was never blocked
💡 Main thread never waits; async method runs in separate thread and completes independently
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
asyncMethod runningfalsetrue (async task started)true (sleeping)false (completed)false
CompletableFuture statenot createdcreated (not done)created (not done)completedcompleted
Key Moments - 3 Insights
Why does the main thread not wait for the async method to finish?
Because @Async tells Spring to run the method in a new thread, so the main thread continues immediately as shown in execution_table step 1.
How can the caller get the result of the async method?
The method returns a CompletableFuture, which the caller can check or wait on later, as shown in execution_table step 4.
What happens if @Async is missing?
The method runs on the main thread and blocks until completion, unlike the async behavior shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the main thread immediately after calling asyncMethod()?
AIt waits for the async method to finish
BIt continues immediately without waiting
CIt throws an error
DIt sleeps for 1 second
💡 Hint
See execution_table step 1 where main thread continues without waiting
At which step does the async method complete its work and return the result?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check execution_table step 3 where method completes after sleep
If the @Async annotation is removed, how would the main thread behave?
AIt would block and wait for the method to finish
BIt would run the method in a new thread
CIt would continue immediately without waiting
DIt would throw a compilation error
💡 Hint
Refer to key_moments answer 3 about missing @Async causing blocking
Concept Snapshot
@Async annotation runs method in a new thread
Main thread continues immediately without waiting
Method returns CompletableFuture for result
Caller can check or wait on CompletableFuture
Without @Async, method runs synchronously
Use @EnableAsync to activate async support
Full Transcript
When you add @Async to a method in Spring Boot, it runs that method in a separate thread. This means the main thread that called it does not wait and keeps running. The async method can return a CompletableFuture, which lets the caller check or wait for the result later. The execution table shows the main thread calling the method, Spring creating a new thread, the async method running and sleeping for 1 second, then completing and returning the result. The main thread never blocks. If you remove @Async, the method runs on the main thread and blocks until done. Remember to enable async support with @EnableAsync in your configuration.