线程池的配置,线程池隔离

线程池的配置:

@Configuration
public class ExecutorConfig {
    @Bean(name = "myExecutor")
    public Executor executor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(3);
        //配置最大线程数
        executor.setMaxPoolSize(10);
        //配置队列大小
        executor.setQueueCapacity(100);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix("MyExecutor-");

        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }
}

@Configuration: 表名这是一个配置类。该类中打上 @Bean 注解的方法都会在 Spring 启动时被扫描运行,然后将返回的 bean 注入到 Spring 容器中。
@Bean: 该方法返回的对象将被注入到 Spring 容器中。在上面的方法中,我们将自定义配置的线程池命名为 myExecutor 交给 Spring 来管理。

在此基础上,可以建立两个线程池,不同业务使用不同的线程

线程池隔离:

@Configuration
public class ThreadPoolConfig {

    @Value("${myThreadPool.maxPoolSize}")
    private Integer maxPoolSize;
    @Value("${myThreadPool.queueCapacity}")
    private Integer queueCapacity;
    @Value("${myThreadPool.keepAliveSeconds}")
    private Integer keepAliveSeconds;
    @Value("${myThreadPool.waitForTasksToCompleteOnShutdown}")
    private Boolean waitForTasksToCompleteOnShutdown;

    @Bean("orderPool")
    public Executor orderExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数等于系统核数
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        // 设置核心线程数
        executor.setCorePoolSize(availableProcessors);
        // executor.setCorePoolSize(corePoolSize);
        // 设置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        // 设置队列大小
        executor.setQueueCapacity(queueCapacity);
        // 设置线程活跃时间(秒)
        executor.setKeepAliveSeconds(keepAliveSeconds);
        // 线程满了之后由调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 设置默认线程名称
        executor.setThreadNamePrefix("threadPool");
        // 等待所有任务执行完成后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown);
        // 执行初始化
        executor.initialize();
        return executor;
    }

    @Bean("productPool")
    public Executor productExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        executor.setCorePoolSize(availableProcessors);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("productPool");
        executor.setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown);
        executor.initialize();
        return executor;
    }
}

使用方法:

@Service
@Slf4j
@EnableAsync
public class OrderServiceImpl implements OrderService { @Async("orderPool") @Override public void getOrder() { log.info("getOrder, current thread is {}",Thread.currentThread().getName()); } }

 

@Service
@Slf4j
@EnableAsync
public class ProductServiceImpl implements ProductService {
    @Async("productPool")
    @Override
    public void getProduct() {
        log.info("getProduct, current thread is {}",Thread.currentThread().getName());
    }
}

 

posted @ 2022-08-16 14:39  二次元的程序猿  阅读(598)  评论(0编辑  收藏  举报