What is the purpose of the @EnableAsync annotation in Spring Boot?
The @EnableAsync annotation tells Spring Boot to enable support for running methods asynchronously, meaning those methods can run in the background without blocking the main thread.
Click to reveal answer
beginner
How do you mark a method to run asynchronously after enabling @EnableAsync?
You add the @Async annotation to the method you want to run asynchronously. This tells Spring to run that method in a separate thread.
Click to reveal answer
intermediate
Where should you place the @EnableAsync annotation in a Spring Boot application?
You usually place <code>@EnableAsync</code> on a configuration class or the main application class to enable asynchronous processing across the app.
Click to reveal answer
advanced
What happens if you call an <code>@Async</code> method from within the same class without going through Spring's proxy?
The @Async method will not run asynchronously because Spring uses proxies to manage async calls. Calling it internally bypasses the proxy, so it runs synchronously.
Click to reveal answer
intermediate
Can you customize the thread pool used by @EnableAsync? How?
Yes, you can customize the thread pool by defining a TaskExecutor bean and specifying it in the @Async annotation or by overriding the default executor in the configuration.
Click to reveal answer
What does the @EnableAsync annotation do in a Spring Boot app?
AEnables asynchronous method execution
BDisables asynchronous processing
CEnables synchronous method execution
DConfigures database connections
✗ Incorrect
@EnableAsync activates support for running methods asynchronously in Spring Boot.
Which annotation marks a method to run asynchronously after enabling @EnableAsync?
A@Async
B@AsyncMethod
C@EnableAsyncMethod
D@RunAsync
✗ Incorrect
The @Async annotation marks methods to run asynchronously.
Where is the best place to put the @EnableAsync annotation?
AOn every asynchronous method
BOn the controller methods only
COn the database entity classes
DOn a configuration or main application class
✗ Incorrect
@EnableAsync is placed on a configuration or main class to enable async support app-wide.
What happens if you call an @Async method from the same class internally?
AIt causes a compile error
BIt runs asynchronously as expected
CIt runs synchronously, not asynchronously
DIt throws a runtime exception
✗ Incorrect
Internal calls bypass Spring proxies, so @Async methods run synchronously.
How can you customize the thread pool used by @EnableAsync?
ABy setting a property in <code>application.properties</code> only
BBy defining a custom <code>TaskExecutor</code> bean
CBy adding <code>@ThreadPool</code> annotation
DYou cannot customize the thread pool
✗ Incorrect
You customize the thread pool by defining a TaskExecutor bean in your configuration.
Explain how @EnableAsync and @Async work together in Spring Boot to run methods asynchronously.
Think about how Spring knows which methods to run in the background.
You got /4 concepts.
Describe a common pitfall when calling an @Async method from within the same class and how to avoid it.
Consider how Spring manages async calls behind the scenes.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @EnableAsync annotation in a Spring Boot application?
easy
A. To enable automatic database transactions
B. To enable REST API endpoint scanning
C. To enable running methods asynchronously in the background
D. To enable security features like authentication
Solution
Step 1: Understand what @EnableAsync does
This annotation activates Spring's ability to run methods asynchronously, meaning in the background without blocking the main thread.
Step 2: Compare with other options
Other options relate to transactions, REST scanning, or security, which are unrelated to @EnableAsync.
Final Answer:
To enable running methods asynchronously in the background -> Option C
Quick Check:
@EnableAsync = run methods asynchronously [OK]
Hint: Remember: Enable async means run tasks in background [OK]
Common Mistakes:
Confusing @EnableAsync with transaction management
Thinking it enables REST endpoints
Assuming it controls security features
2. Which of the following is the correct way to enable asynchronous method execution in a Spring Boot application?
easy
A. Add @Async on configuration class and @EnableAsync on methods
B. Add @Async on the main application class only
C. Add @EnableAsync on every method you want async
D. Add @EnableAsync on a configuration class and @Async on methods
Solution
Step 1: Identify correct annotation placement
@EnableAsync must be on a configuration or main class to activate async support globally.
Step 2: Mark methods with @Async
Methods that should run asynchronously need the @Async annotation.
Final Answer:
Add @EnableAsync on a configuration class and @Async on methods -> Option D
Quick Check:
Enable async globally + mark methods async = Add @EnableAsync on a configuration class and @Async on methods [OK]
Hint: Enable async on config class, async on methods [OK]
Common Mistakes:
Putting @Async on the main class only
Using @EnableAsync on methods instead of config
Mixing up @Async and @EnableAsync roles
3. Given the code below, what will be the output when mainMethod() is called?
@EnableAsync
@Configuration
public class AppConfig {}
@Service
public class MyService {
@Async
public void asyncMethod() {
System.out.println("Async task started");
}
public void mainMethod() {
System.out.println("Main method start");
asyncMethod();
System.out.println("Main method end");
}
}
medium
A. Main method start
Main method end
Async task started
B. Main method start
Async task started
Main method end
C. Async task started
Main method start
Main method end
D. Main method start
Main method end
Solution
Step 1: Understand async method behavior
The @Async method runs asynchronously only when called from a different Spring-managed bean proxy. Calling asyncMethod() directly within the same class does NOT run asynchronously, so it executes synchronously.
Step 2: Trace output order
All print statements execute in order: "Main method start", "Async task started", then "Main method end".
Final Answer:
Main method start
Async task started
Main method end -> Option B
Quick Check:
Direct self-invocation bypasses async proxy, so method runs synchronously [OK]
Hint: Async methods called within same class run synchronously [OK]
Common Mistakes:
Assuming async prints immediately in order
Thinking async blocks main thread
Ignoring that async runs in background thread only when called via proxy
4. Consider this Spring Boot setup:
@Configuration
public class AsyncConfig {
@Async
public void runAsync() {
System.out.println("Running async");
}
}
What is the main problem with this code?
medium
A. Missing @EnableAsync annotation to activate async support
B. Methods annotated with @Async must return a value
C. @Async cannot be used inside @Configuration classes
D. The method must be static to run asynchronously
Solution
Step 1: Check if async support is enabled
Without @EnableAsync on a configuration or main class, Spring will not run @Async methods asynchronously.
Step 2: Validate other options
@Async methods can be void, can be in any Spring bean, and do not need to be static.
Final Answer:
Missing @EnableAsync annotation to activate async support -> Option A
Quick Check:
Enable async missing = async won't run [OK]
Hint: Always add @EnableAsync to activate async methods [OK]
Common Mistakes:
Thinking @Async needs return value
Believing @Async can't be in config classes
Assuming method must be static
5. You want to run multiple slow tasks in parallel in a Spring Boot app. Which setup correctly uses @EnableAsync and @Async to achieve this?
@Configuration
@EnableAsync
public class AsyncConfig {}
@Service
public class TaskService {
@Async
public CompletableFuture<String> slowTask(String name) {
Thread.sleep(2000);
return CompletableFuture.completedFuture(name + " done");
}
}
// In some controller or service
public void runTasks() {
CompletableFuture<String> t1 = taskService.slowTask("Task1");
CompletableFuture<String> t2 = taskService.slowTask("Task2");
CompletableFuture.allOf(t1, t2).join();
System.out.println("All tasks completed");
}
hard
A. This setup runs slowTask methods in parallel without blocking main thread
B. This setup runs slowTask methods sequentially blocking main thread
C. Missing @Async on slowTask method, so runs sequentially
D. Missing @EnableAsync, so async calls run synchronously
Solution
Step 1: Check annotations and method return type
@EnableAsync is present to enable async support, and @Async marks slowTask to run asynchronously returning CompletableFuture.
Step 2: Understand method calls and joining
Calling slowTask twice starts both tasks in parallel threads. CompletableFuture.allOf(...).join() waits for both to finish without blocking main thread execution during tasks.
Final Answer:
This setup runs slowTask methods in parallel without blocking main thread -> Option A