package com.bohuikeji.frame.module.admin.thread;

import cn.hutool.core.thread.NamedThreadFactory;

import java.util.concurrent.*;

import static java.lang.Integer.MAX_VALUE;

/**
 * 线程池工厂类
 *
 * @author 10552
 */
public class CachedThreadPool {

    /**
     * 静态内部类单例，初始话线程池实列
     */
    private static class ThreadPoolHolder {
        //定义一个线程池，静态内部类单例方式
        private static final ExecutorService exec;

        static {
            /**
             * 实例化缓存线程池
             * 适用于大量短生命周器类型的任务
             */
            exec = new ThreadPoolExecutor(4, MAX_VALUE,
                    60L, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>(),new NamedThreadFactory("ranch线程池",false));
        }
    }

    /**
     * 执行线程任务
     *
     * @param command 一个需要执行的线程任务
     */
    public static void execute(Runnable command) throws Exception {
        ThreadPoolHolder.exec.execute(command);
    }
}
