Async & Scheduling
Async execution with @Async, scheduling with @Scheduled, ThreadPoolTaskExecutor, time management
1Which annotation makes a method asynchronous in Spring?
Which annotation makes a method asynchronous in Spring?
回答
@Async marks a method for asynchronous execution. Spring executes this method in a separate thread via a TaskExecutor. Requires @EnableAsync on a configuration class to enable the mechanism. Useful for long operations (email sending, file processing) without blocking the main thread. Method can return void, Future or CompletableFuture.
2Which annotation enables support for @Async methods in Spring?
Which annotation enables support for @Async methods in Spring?
回答
@EnableAsync activates processing of @Async annotations. It must be placed on a @Configuration class. Without this annotation, @Async is ignored and methods execute synchronously.
3Which legacy return type allows retrieving the result of an @Async method?
Which legacy return type allows retrieving the result of an @Async method?
回答
Future<T> is the historical return type for async methods. Allows retrieving the result via future.get() (blocking). CompletableFuture<T> is now preferred for its functional programming and better composition.
What is the main advantage of CompletableFuture over Future for @Async methods?
What is AsyncResult used for in Spring?
+22 面接問題