package org.jeecg.modules.common.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

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

/**
 * @description: 管理线程池
 * @author: niugeng
 * @create: 2023-05-05 09:47
 **/
public class ThreadExecutorsUtil {
    /**
     * 引入依赖包，创建线程池
     */
    private static final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("thread-log-runner-%d").build();

    public static final ThreadPoolExecutor executor =  new ThreadPoolExecutor(10, 50, 200L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);

    public static final ThreadPoolExecutor executor5 =  new ThreadPoolExecutor(5, 5, 200L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);

    //不加 会提示错误：there is no session with id 【xxxxx】 系统环境：springboot mysql mybatis shiro linux
    static {
        //允许核心线程空闲时，过了一定的时间自动销毁
        executor.allowCoreThreadTimeOut(true);
        //设置线程空闲时间为1秒  1秒后便销毁
        executor.setKeepAliveTime(1,TimeUnit.SECONDS);
    }




}
