package cn.sikey.mcdisk.config;

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

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: nelson
 * @Date: 2025/5/30
 * @Description: 线程池
 */
@Configuration
public class ThreadPoolConfig {
    @Bean(name = "myTaskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数（默认线程数）
        executor.setCorePoolSize(5);
        // 最大线程数
        executor.setMaxPoolSize(10);
        // 队列容量（当核心线程满时，新任务进入队列）
        executor.setQueueCapacity(100);
        // 线程池名前缀
        executor.setThreadNamePrefix("my-async-thread");
        // 拒绝策略：由调用者线程（主线程）执行任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化线程池
        executor.initialize();
        return executor;
    }
}
