package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Mr_gzy
 */
@Slf4j
public class AsyncManager {
    private final ScheduledExecutorService executorService;

    private AsyncManager() {
        // 创建线程池实例
        this.executorService = Executors.newScheduledThreadPool(10);
    }

    public static AsyncManager me() {
        // 单例模式实现
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        private static final AsyncManager INSTANCE = new AsyncManager();
    }

    public void shutdown() {
        // 关闭线程池
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
            try {
                if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                executorService.shutdownNow();
                Thread.currentThread().interrupt();
            }
        }
        log.info("====后台任务线程池已关闭====");
    }
}
