Spring Boot FAQ: Top Questions
35. How does Spring Boot support async processing with @Async?
Spring Boot supports asynchronous processing using @Async
annotation, which allows methods to run in separate threads.
πΊοΈ Setup:
- Enable async support using
@EnableAsync
. - Annotate methods with
@Async
.
π₯ Example:
@Async
public void asyncMethod() {
Thread.sleep(5000);
System.out.println("Completed async task");
}
π Expected Output:
Method runs asynchronously without blocking caller thread.
π οΈ Use Cases:
- Sending emails or notifications in the background.
- Parallel execution of independent processes.