package com.cool.store.executor;

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;

/**
 * @Author suzhuhong
 * @Date 2025/10/15 17:29
 * @Version 1.0
 */
@Configuration
public class ThreadPoolTask {

    @Bean
    public TaskExecutor noticeThreadPool() {
        int cores = Runtime.getRuntime().availableProcessors();

        ThreadPoolTaskExecutor executor = new MdcTaskExecutor();
        // 核心线程数目
        executor.setCorePoolSize(cores*2);
        // 指定最大线程数
        executor.setMaxPoolSize(100);
        // 队列中最大的数目
        executor.setQueueCapacity(10000);
        // 线程名称前缀
        executor.setThreadNamePrefix("noticeThreadPool_");
        // 对拒绝task的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 线程空闲后的最大存活时间
        executor.setKeepAliveSeconds(60);
        // 加载
        executor.initialize();
        return executor;
    }



}
