Spring线程池ThreadPoolTaskExecutor配置示例代码

在需要使用的方法上添加注解 @Async(value = "threadPoolSaveVehiclePass") 即可。

package com.communication.dataaccess.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName ThreadPoolConfig
 * @Description 线程池配置类
 * @Author admin
 * @Date 2022/2/23 15:31
 * @Version: 2.0
 */
@EnableAsync
@Configuration
public class ThreadPoolConfig {

    /**
     * @title: obtainVehiclePassThreadPool
     * @author: admin
     * @createDate: 2022/2/25 10:05
     * @version: 1.0
     * @return: java.util.concurrent.Executor
     * @description: 数据获取连接池
     */
    @Bean(name ="threadPoolObtainVehiclePass")
    public Executor obtainVehiclePassThreadPool() {
        ThreadPoolTaskExecutor executor =new ThreadPoolTaskExecutor();
        //核心线程数
        executor.setCorePoolSize(2);
        //最大线程数
        executor.setMaxPoolSize(4);
        //队列容量
        executor.setQueueCapacity(1000);
        //允许的空闲时间(秒)
        executor.setKeepAliveSeconds(60);
        //线程名称前缀
        executor.setThreadNamePrefix("ThreadPool-obtainVehiclePass-");
        /**
         * 线程策略:
         * AbortPolicy          丢弃任务并抛出RejectedExecutionException异常
         * CallerRunsPolicy     由调用线程处理该任务
         * DiscardOldestPolicy  丢弃队列最前面的任务然后重新尝试执行任务(重复此过程)
         * DiscardPolicy        丢弃被拒绝的任务
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //线程实例化
        executor.initialize();
        return executor;
    }

    /**
     * @title: saveVehiclePassThreadPool
     * @author: admin
     * @createDate: 2022/2/25 10:05
     * @version: 1.0
     * @return: java.util.concurrent.Executor
     * @description: 数据存储连接池
     */
    @Bean(name ="threadPoolSaveVehiclePass")
    public Executor saveVehiclePassThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数
        executor.setCorePoolSize(4);
        //最大线程数
        executor.setMaxPoolSize(8);
        //队列容量
        executor.setQueueCapacity(1000);
        //允许的空闲时间(秒)
        executor.setKeepAliveSeconds(60);
        //线程名称前缀
        executor.setThreadNamePrefix("ThreadPool-saveVehiclePass-");
        /**
         * 线程策略:
         * AbortPolicy          丢弃任务并抛出RejectedExecutionException异常
         * CallerRunsPolicy     由调用线程处理该任务
         * DiscardOldestPolicy  丢弃队列最前面的任务然后重新尝试执行任务(重复此过程)
         * DiscardPolicy        丢弃被拒绝的任务
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //线程实例化
        executor.initialize();
        return executor;
    }
}
上一篇:Hello SpringBoot(搭建springboot)


下一篇:Numpy的常用方法