package com.ruoyi.projetctCost.ThreadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SingletonThreadPool {
    private static volatile SingletonThreadPool instance;
    private final ThreadPoolExecutor executor;
    private SingletonThreadPool() {
        // 私有构造方法
        int corePoolSize = 10; // 核心线程池大小
        int maxPoolSize = 20; // 最大线程池大小
        long keepAliveTime = 60L; // 线程空闲时间
        TimeUnit unit = TimeUnit.SECONDS; // 线程空闲时间单位
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100); // 任务队列
        executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue);
    }
    public static SingletonThreadPool getInstance() {
        if (instance == null) {
            synchronized (SingletonThreadPool.class) {
                if (instance == null) {
                    instance = new SingletonThreadPool();
                }
            }
        }
        return instance;
    }
    public ThreadPoolExecutor getExecutor() {
        return executor;
    }
}