线程池--配置和在spring中使用,@PropertySource读取配置文件

1.注入到容器中

/**
 * 线程池配置
 *
 * @date 2019年02月27日
 */
@Configuration
@PropertySource("classpath:/threadpool.properties")
public class ExecutePoolConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(ExecutePoolConfiguration.class);

    @Value("${threadpool.core-pool-size}")
    private int corePoolSize;

    @Value("${threadpool.max-pool-size}")
    private int maxPoolSize;

    @Value("${threadpool.queue-capacity}")
    private int queueCapacity;

    @Value("${threadpool.keep-alive-seconds}")
    private int keepAliveSeconds;

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setKeepAliveSeconds(keepAliveSeconds);
        // 核心线程池数
        pool.setCorePoolSize(corePoolSize);
        // 最大线程
        pool.setMaxPoolSize(maxPoolSize);
        // 队列容量
        pool.setQueueCapacity(queueCapacity);
        // 队列满,线程被拒绝执行策略
        pool.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        return pool;
    }
}

2.使用

    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    threadPoolTaskExecutor.execute(new SaveLogThread(sysLog, sysLogService));
原文地址:https://www.cnblogs.com/kltsee/p/13949128.html