Hackforge Academy

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

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community