package com.njzscloud.common.core.utils;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class Scheduler {
    private static ScheduledExecutorService scheduler;

    public static void init(int corePoolSize) {
        scheduler = Executors.newScheduledThreadPool(corePoolSize);
    }

    public static void shutdown() {
        if (scheduler != null) {
            scheduler.shutdown();
            scheduler = null;
        }
        log.info("定时器已关闭");
    }

    public static void init() {
        scheduler = Executors.newScheduledThreadPool(1);
        log.info("定时器初始化成功");
    }

    public static void setInterval(Runnable command, long period) {
        scheduler.scheduleAtFixedRate(command, 0, period, TimeUnit.SECONDS);
    }

    public static void setInterval(Runnable command) {
        scheduler.submit(command);
    }

    public static void setTimeout(Runnable command, long delay) {
        scheduler.schedule(command, delay, TimeUnit.SECONDS);
    }
}
