ScheduledExecutorService用法

it2024-12-01  12

ScheduledExecutorService用法

初始化

简单用法

public class TestTask { /** * 定时任务执行器 */ private ScheduledExecutorService executorService; /** * 定时任务间隔时间 */ private long taskTime; public TestTask(int threadSize, long taskTime) { this.beatTaskTime = beatTaskTime; executorService = new ScheduledThreadPoolExecutor(threadSize, (r) -> { Thread thread = new Thread(r); thread.setDaemon(true); thread.setName("com.wmang.service.test"); return thread; }); } public void initBeatTask() { executorService.scheduleAtFixedRate(() -> { // 定时任务执行逻辑 }, beatTaskTime,beatTaskTime, TimeUnit.MILLISECONDS); } }

方法解释

schedule(Runnable command, long delay, TimeUnit un

最新回复(0)