Spring Boot handles asynchronous operations by executing tasks in separate threads so that the main application thread does not have to wait for those tasks to complete. This improves application responsiveness and allows long-running operations to run in the background without blocking user requests.
Key Points: • @EnableAsync enables asynchronous method execution in the application. • @Async runs a method in a separate thread managed by Spring. • Custom thread pools can be configured to improve performance and resource utilization.
Example: Consider an online shopping application where a user places an order.
Synchronous Flow:
Place Order ↓ Save Order ↓ Send Email ↓ Generate Invoice ↓ Send SMS ↓ Return Response
The user waits until all tasks finish.
Asynchronous Flow:
Place Order ↓ Save Order ↓ Return Response Immediately ↓ Send Email (Background) Generate Invoice (Background) Send SMS (Background)
The user receives an immediate response while background tasks continue processing.
Code Example:
@SpringBootApplication
@EnableAsync
public class Application {
}
@Service
public class NotificationService {
@Async
public void sendEmail(String email) {
// Email sending logic
}
}Spring automatically executes the sendEmail() method in a separate thread.
Supported Return Types:
• void • Future<T> • CompletableFuture<T> • ListenableFuture<T>
Example:
@Async
public CompletableFuture<String> generateReport() {return CompletableFuture.completedFuture(
"Report Generated Successfully");
}This allows the caller to retrieve the result when processing completes.
Custom Thread Pool Configuration:
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor =
new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(100);
executor.initialize();
return executor;
}Benefits: • Better control over thread usage. • Improved scalability. • Prevents thread exhaustion.
Important Limitation:
@Async works through Spring proxies.
This will NOT execute asynchronously:
@Service
public class UserService {
public void registerUser() {
sendEmail();
}
@Async
public void sendEmail() {
}
}Reason: • Internal method calls bypass Spring proxies.
Correct Approach: • Invoke the @Async method from another Spring-managed bean.
Common Use Cases:
• Email Notifications • SMS Processing • File Upload Processing • Report Generation • Audit Logging • Background Data Synchronization
Real-World Example:
Banking Application:
Customer Transfers Money ↓ Transaction Saved ↓ Response Returned ↓ SMS Notification Sent Asynchronously Email Receipt Generated Asynchronously
This improves customer experience by reducing response time.
Interview Tip: A concise interview answer is: Spring Boot handles asynchronous operations using @EnableAsync and @Async annotations. Methods marked with @Async execute in separate threads managed by Spring, allowing long-running tasks such as email sending or report generation to run in the background without blocking the main application flow. For production applications, custom thread pools are recommended for better performance and scalability.