Java 21 has officially supported the feature of virtual threads, and I am very excited to use this feature in Spring Boot
I've come across a statement: "We should not treat virtual threads the same way as platform threads by using pools to constrain them because they are very lightweight resources that can be discarded once they are used up."
Is there any other way to make Spring Boot's @Async annotation use virtual threads to handle asynchronous tasks without needing to register a virtual thread pool like the following?
@EnableAsync
@Configuration
public class AsyncThreadConfiguration {
@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
}
@Bean
public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
return protocolHandler -> protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
}
}
I look forward to a simple configuration approach that allows the @Async annotation to use virtual threads to execute asynchronous tasks without the need for a thread pool.