package com.face.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.threads.TaskThreadFactory;

import java.util.concurrent.*;

/**
 * @Description:
 * @Author: zhaikaibo
 * @Date: 2019/7/10 14:02
 */
public class SingleThreadPoolUtil {

    private static String corePoolSize = SpringUtil.getEnvironment().getProperty("dap.flow.parse.load.corePoolSize");
    private static String keepAliveTime = SpringUtil.getEnvironment().getProperty("dap.flow.parse.load.keepAliveTime");

    private static String threadFactoryName = "edu";

    private static TaskThreadFactory threadFactory = new TaskThreadFactory(threadFactoryName, false, 5);

    private static final ThreadPoolExecutor singleThreadPoolExecutor = new ThreadPoolExecutor(
            StringUtils.isBlank(corePoolSize) ? 10 : Integer.valueOf(corePoolSize),
            Integer.valueOf(Integer.MAX_VALUE),
            StringUtils.isBlank(keepAliveTime) ? 10L : Long.valueOf(keepAliveTime),
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(),threadFactory);


    private SingleThreadPoolUtil() {
    }

    /**
     * 获取线程工具实例
     */
    public static SingleThreadPoolUtil getInstance() {
        return new SingleThreadPoolUtil();
    }

    /**
     * 获取线程池实例
     * @return
     */
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        return singleThreadPoolExecutor;
    }

    /**
     * 执行任务
     * @param task
     * @return
     */
    public static Future execute(Callable task) {
        return singleThreadPoolExecutor.submit(task);

    }

    /**
     * 关闭线程池
     * 注意：此线程池为final的，此方法慎用
     */
    public static void shutdown(){
        if(singleThreadPoolExecutor!=null||!singleThreadPoolExecutor.isShutdown()){
            singleThreadPoolExecutor.shutdown();
        }
    }


}
