package top.octopusyan.manager;

import java.util.concurrent.*;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2020/01/11
 * desc   : 线程池管理类
 */
public final class ThreadPoolManager extends ThreadPoolExecutor {

    private static volatile ThreadPoolManager sInstance;

    private static ScheduledExecutorService scheduledExecutorService;

    private ThreadPoolManager() {
        super(0, Integer.MAX_VALUE,
                30L, TimeUnit.SECONDS,
                new SynchronousQueue<>());
    }

    public static ThreadPoolManager getInstance() {
        if(sInstance == null) sInstance = new ThreadPoolManager();
        return sInstance;
    }

    public static ScheduledExecutorService getScheduledExecutorService(int corePoolSize) {
        if (scheduledExecutorService == null) {
            scheduledExecutorService = Executors.newScheduledThreadPool(corePoolSize);
        }
        return scheduledExecutorService;
    }

    public static ScheduledExecutorService getScheduledExecutorService() {
        return getScheduledExecutorService(5);
    }
}