在Java中,我们可以使用ScheduledExecutorService
定期运行一个任务,也可以在TimeUnit
预定义的延迟后一次运行一次。
1.一次运行任务
ScheduledExecutorService
接受Runnable
和Callable
任务。
1.1运行Runnable
5秒初始延迟之后的任务。
ScheduledExecutorExample.java
package com.mkyong.concurrency.executor.scheduler;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorRunnable {
public static void main(String[] args) {
ScheduledExecutorService ses = Executo