这个问题在这里已经有了答案:
How to use multiple threadPoolExecutor for Async Spring
(1 个回答)
2年前关闭。
我现在正在使用 @EnableAsync
和 @Async
在 Spring Boot 中使用多线程的注解。我有服务 A(快)和服务 B(慢)。
如何为他们设置不同的池?因此,当有大量对 B 的调用时,应用程序仍然可以在与 B 不同的池中处理服务 A。
@Configuration
@EnableAsync
public class ServiceExecutorConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(30);
taskExecutor.setMaxPoolSize(40);
taskExecutor.setQueueCapacity(10);
taskExecutor.initialize();
return taskExecutor;
}
}
最佳答案
@Bean(name = "threadPoolExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
executor.setThreadNamePrefix("threadPoolExecutor-");
executor.initialize();
return executor;
}
@Bean(name = "ConcurrentTaskExecutor")
public TaskExecutor taskExecutor2 () {
return new ConcurrentTaskExecutor(
Executors.newFixedThreadPool(3));
}
@Override
@Async("threadPoolExecutor")
public void createUserWithThreadPoolExecutor(){
System.out.println("Currently Executing thread name - " + Thread.currentThread().getName());
System.out.println("User created with thread pool executor");
}
@Override
@Async("ConcurrentTaskExecutor")
public void createUserWithConcurrentExecutor(){
System.out.println("Currently Executing thread name - " + Thread.currentThread().getName());
System.out.println("User created with concurrent task executor");
}
关于java - 如何在 Spring Boot 中创建不同的 ThreadPoolTaskExecutor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57419242/