37 lines
1.2 KiB
Java
37 lines
1.2 KiB
Java
package com.muyu.common.pool;
|
|
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.ScheduledFuture;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* @author DongZl
|
|
* @description: 线程池
|
|
* @Date 2023-11-17 上午 09:16
|
|
*/
|
|
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();
|
|
}
|
|
}
|