package com.rongwei.safecommon.config;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ThreadPoolConfig class
 *
 * @author XH
 * @date 2023/12/18
 */

@Configuration
public class ThreadPoolConfig {
    public static final String NAME_PRE = "custom-";

    @Bean("customThreadPool")
    public static ThreadPoolExecutor getExecutor() {
        return ExecutorBuilder.create()
                .setCorePoolSize(8)
                .setMaxPoolSize(16)
                .setKeepAliveTime(60, TimeUnit.SECONDS)
                .setHandler(new ThreadPoolExecutor.CallerRunsPolicy())
                .setAllowCoreThreadTimeOut(true)
                .setThreadFactory(ThreadFactoryBuilder.create().setNamePrefix(NAME_PRE).build())
                .build();
    }

}
