Category: spring_boot
what is Async Programming
Published on 13 Jun 2026
Explanation
Async Programming in Spring Boot allows
tasks to run in a separate
thread without blocking the main thread.
It is commonly used for email
sending, report generation,
file processing, and
external API calls.
Code:
@EnableAsync
@SpringBootApplication
public class Application {
}
Explanation
Use the @Async annotation on a
method to execute it asynchronously.
Code:
@Service
public class EmailService {
@Async
public void sendEmail() {
System.out.println("
Sending email...");
}
}
Explanation
The calling method continues execution
without
waiting for the async task to
finish.
Code:
@Service
public class OrderService {
@Autowired
private EmailService emailService;
public void processOrder() {
emailService.sendEmail();
System.out.println("Order
processed");
}
}
Explanation
An async method can return a
value using CompletableFuture.
Code:
@Async
public CompletableFuture<String>
generateReport() {
return CompletableFuture.
completedFuture("Report Ready");
}
Explanation
Retrieve the result from the
CompletableFuture
when needed.
Code:
CompletableFuture<String> future = reportService.generateReport(); String result = future.get();
Explanation
Execution Flow: The main thread returns
immediately while the async task runs
in a background thread.
Code:
Request --> Main Thread --> Response
Returned
Main Thread--> Async Thread -->
Long Running Task