package com.xisc.pm.app.service.autoboard.config;

import java.util.concurrent.ThreadPoolExecutor;

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

@Configuration
public class ThreadPoolConfig {

    /** 核心线程池大小 */
    private final int CORE_POOL_SIZE = 8;

    /** 最大可创建的线程数 */
    private final int MAX_POOL_SIZE = 10;

    /** 队列最大长度 */
    private final int QUEUE_CAPACITY = 2000;

    /** 线程池维护线程所允许的空闲时间 */
    private final int KEEP_ALIVE_SECONDS = 300;

    @Bean(name = "calcThreadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}
