java - 如何在 Spring Boot 中创建不同的 ThreadPoolTask​​Executor?

标签 java spring multithreading spring-boot threadpoolexecutor

这个问题在这里已经有了答案:





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 中创建不同的 ThreadPoolTask​​Executor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57419242/

相关文章:

JavaFX 阻止 UI(和代码)直到服务器调用完成

java - Spring Boot 应用程序意外退出并显示 "Killed"

java - 当从 Atmosphere 的 MeteorServlet 中使用 Spring DispatcherServlet 时,Spring 上下文会加载两次

Spring Boot 无法在 ElasticBeanstalk AWS 上找到 jsp 文件

c# - 在后台加载一个窗口,直到告诉它显示

android - 如果引用超出范围,为什么 AsyncTask 不会被垃圾回收?

java - 查找给定 Java 代码的时间和空间复杂度

java - 在浏览器中运行 Java applet 时,是否可以删除对对话框的任何需求?

java - 字符串缓冲区声明不足

c++ - Qt:向死线程/停止线程发送信号