package com.yeejoin.amos.supervision.core.threadpool;

import com.yeejoin.amos.supervision.business.constants.XJConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池
 */
public class AmosThreadPool {

    /**
     * 日志记录器
     */
    private static final Logger log = LoggerFactory.getLogger(AmosThreadPool.class);

    /**
     * 单例
     */
    private static volatile AmosThreadPool instance;

    /**
     * 执行服务
     */
    private static ExecutorService executorService;

    /**
     * 获取单例
     *
     * @return
     */
    public static AmosThreadPool getInstance() {
        if (instance == null) {
            synchronized (AmosThreadPool.class) {
                if (instance == null) {
                    instance = new AmosThreadPool();
                }
            }
        }
        return instance;
    }

    static {
        executorService = Executors
                .newFixedThreadPool(XJConstant.AMOS_THREAD_NUM);
    }

    /**
     * 执行线程
     *
     * @param task
     */
    public void execute(Runnable task) {
        executorService.execute(task);
    }

}
