I think you need @EnableAsync
to enable @Async
annotation and this annotation will use default implementation SimpleAsyncTaskExecutor
SimpleAsyncTaskExecutor
implementation does not reuse any threads, rather it starts up a new thread for each invocation. However, it does support a concurrency limit which will block any invocations that are over the limit until a slot has been freed up.
You can define your own ThreadPoolTaskExecutor
like
@Configuration
public class ThreadConfig {
@Bean("otherExecutor")
public TaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(16);
executor.setMaxPoolSize(32);
executor.initialize();
return executor;
}
}
And refer to this in the @Async
@Async("otherExecutor")
void doSomething(String s) {
// this will be executed asynchronously by "otherExecutor"
}