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