Spring / Spring Boot 4 Basics Interview Questions
How does Spring Boot 4 handle async processing with @Async?
@Async makes a method execute in a separate thread asynchronously, returning a CompletableFuture (or void) immediately while processing continues in the background. Spring Boot 4 automatically uses virtual threads when enabled.
// Enable async processing: @SpringBootApplication @EnableAsync public class App { ... } // Async service: @Service public class EmailService { // Fire-and-forget (no return value): @Async public void sendWelcomeEmail(String email) { // Runs in separate thread; caller continues immediately emailGateway.send(email, "Welcome!", buildWelcomeBody(email)); } // Async with result: @Async public CompletableFuture<EmailStats> getEmailStats(String userId) { EmailStats stats = emailGateway.fetchStats(userId); // slow I/O return CompletableFuture.completedFuture(stats); } } // Caller: @Service public class RegistrationService { public User register(RegisterRequest req) { User user = userRepo.save(new User(req)); emailService.sendWelcomeEmail(user.email()); // returns immediately return user; // registration complete; email sends in background } // Parallel async calls: public DashboardData loadDashboard(String userId) throws Exception { CompletableFuture<OrderStats> orders = orderService.getStats(userId); CompletableFuture<EmailStats> emails = emailService.getEmailStats(userId); CompletableFuture<PaymentInfo> payment = paymentService.getInfo(userId); // Wait for all 3 to complete (run in parallel) CompletableFuture.allOf(orders, emails, payment).join(); return new DashboardData(orders.get(), emails.get(), payment.get()); } } # Custom async executor thread pool: spring: task: execution: pool: core-size: 5 max-size: 20 queue-capacity: 100 thread-name-prefix: async-
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
