1
下面的代碼:程序爲什麼要等待schedule()完成,但不等待scheduleWithFixedDelay()?
ScheduledExecutorService service = null;
try {
service = Executors.newSingleThreadScheduledExecutor();
Runnable task1 =() -> System.out.println("Executed only once");
Runnable task2 =() -> System.out.println("Executed repeatedly");
service.schedule(task1, 5, TimeUnit.SECONDS);
service.scheduleWithFixedDelay(task2, 6, 2, TimeUnit.SECONDS);
} finally {
if (service != null) {
service.shutdown();
}
}
當執行上面的代碼程序等待5秒至運行時間表(),但之後它完成,而無需運行scheduleWithFixedDelay()。
我懷疑原因是schedule()與scheduleWithFixedDelay()同步執行,但我沒有在文檔中找到有利於此的參數。