package com.muyu.web.common;

import net.sf.jsqlparser.statement.select.KSQLWindow;

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

public class ScheduledThreadPool {
    /**
     * 周期线程数 CPU*2+1
     */
    private static final ScheduledExecutorService scheduledThreadPool= Executors.newScheduledThreadPool(
            Runtime.getRuntime().availableProcessors()*2+1
    );

    public static ScheduledFuture<?>submit(Runnable thread){
        // 参数分别是： 任务， 多久后开始执行， 每隔多久执行一次（周期），时间单位
        return submit(thread, 1);
    }

    public static ScheduledFuture<?>submit(Runnable thread,long period){
        return scheduledThreadPool.scheduleAtFixedRate(thread,0,period, TimeUnit.SECONDS);
    }

    /**
     * 关闭线程池
     */
    public static void shutdown(){
        scheduledThreadPool.shutdown();
    }
}
