1.ScheduledExecutorService接口各实现类
2.ScheduledExecutorService接口下的方法
代码块:
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
System.out.println("调用开始...");
long start = System.currentTimeMillis();
scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("运行间隔..." + (System.currentTimeMillis() - start) / 1000 + "s");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}, 10l, 2l, TimeUnit.SECONDS); // 延迟十秒后,每隔两秒运行一次
运行结果:
调用开始...
运行间隔...10s
2016-12-06 23:55:25
运行间隔...12s
2016-12-06 23:55:27
运行间隔...14s
2016-12-06 23:55:29
运行间隔...16s
2016-12-06 23:55:31
运行间隔...18s
2016-12-06 23:55:33
运行间隔...20s
2016-12-06 23:55:35
运行间隔...22s
2016-12-06 23:55:37
运行间隔...24s
2016-12-06 23:55:39
运行间隔...26s
2016-12-06 23:55:41