package com.ynfy.common.utils;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtil {
    private ThreadPoolUtil() {

    }

    private static final int CORE_POOL_SIZE = 20;

    private static final int MAXIMUM_POOL_SIZE = 20;

    private static final long KEEP_ALIVE_TIME = 60;

    private static final int BLOCKING_QUEUE_LENGTH = 1000;

    private static ThreadPoolExecutor executor;

    public static ThreadPoolExecutor getExecutor() {
        if (executor == null) {
            synchronized (ThreadPoolExecutor.class) {
                if (executor == null) {
                    executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME,
                            TimeUnit.SECONDS, new LinkedBlockingDeque<>(BLOCKING_QUEUE_LENGTH));
                }
            }
        }
        return executor;
    }
}
