What is Spring Boot @Async and thread management?
Why Interviewers Ask This
This question targets practical, hands-on experience with Spring Boot. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
@Async makes Spring execute a method asynchronously in a separate thread, returning immediately to the caller. Enable: @EnableAsync on @Configuration. Usage: @Async public CompletableFuture<User> findUserAsync(Long id) { // Runs in separate thread return CompletableFuture.completedFuture(userRepository.findById(id).orElseThrow()); } @Async public void sendEmailAsync(String to, String subject) { // Fire and forget // emailService.send(to, subject); } // Calling async method: CompletableFuture<User> future = userService.findUserAsync(1L); User user = future.get(); // blocks until complete, or: future.thenAccept(u -> log.info("Found: {}", u.getName()));. Custom thread pool: by default, uses SimpleAsyncTaskExecutor (creates a new thread per task). Configure a proper pool: @Bean(name = "taskExecutor") public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(20); executor.setQueueCapacity(100); executor.setThreadNamePrefix("Async-"); executor.setRejectedExecutionHandler(new CallerRunsPolicy()); executor.initialize(); return executor; } @Async("taskExecutor") // specify the executor. Exception handling in @Async: exceptions in void methods are lost. Use CompletableFuture and handle exceptions on the caller side, or implement AsyncUncaughtExceptionHandler. Limitations: @Async only works when called from a different Spring bean (same class = no proxy). @Async methods must be public. Common use cases: sending emails, notifications, generating reports, long-running background tasks, parallel external API calls.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.
Previous
What is Spring Boot Flyway/Liquibase database migration?
Next
What is Spring Boot testing strategies?
More Spring Boot Questions
View all →- Intermediate What is Spring AOP (Aspect-Oriented Programming)?
- Intermediate What is Spring Boot caching with @Cacheable?
- Intermediate What is Spring Data JPA query methods and JPQL?
- Intermediate What is Spring Boot REST API best practices?
- Intermediate What is Spring Boot JWT authentication implementation?