ThreadConfig.java 1.13 KB
package com.viontech.keliu.config;

import cn.hutool.core.thread.RejectPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadConfig {

    @Value("${thread.max.pool.size:40}")
    private Integer maxPoolSize;
    @Value("${thread.core.pool.size:20}")
    private Integer centerPoolSize;

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setCorePoolSize(centerPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(50);
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskExecutor.setThreadNamePrefix("personGroup-");
        return threadPoolTaskExecutor;
    }
}