ThreadPoolTaskExecutorConfig.java 1.81 KB
package com.topdraw.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/*@Configuration
@PropertySource(value = {"classpath:executor.properties"}, ignoreResourceNotFound=false, encoding="UTF-8")
@Slf4j*/
public class ThreadPoolTaskExecutorConfig {

    /*@Value("${threadPoolExecutor.core_pool_size}")
    private int corePoolSize;
    @Value("${threadPoolExecutor.max_pool_size}")
    private int maxPoolSize;
    @Value("${threadPoolExecutor.queue_capacity}")
    private int queueCapacity;
    @Value("${threadPoolExecutor.name.prefix}")
    private String namePrefix;
    @Value("${threadPoolExecutor.keep_alive_seconds}")
    private int keepAliveSeconds;

    @Bean
    public ThreadPoolTaskExecutor executorTask(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(corePoolSize);
        // 最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        // 任务队列大小
        executor.setQueueCapacity(queueCapacity);
        // 线程前缀名
        executor.setThreadNamePrefix(namePrefix);
        // 线程的空闲时间
        executor.setKeepAliveSeconds(keepAliveSeconds);
        // 拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 线程初始化
        executor.initialize();
        return executor;
    }*/

}