「fixedRate」:在開始下一次執行之前,從上次執行的開始等待X毫秒。如果當前執行超過'fixedRate'時間間隔,則下一個執行將排隊,但只有下一個執行。它不會產生一系列排隊執行
的
private static int i = 0;
@Scheduled(initialDelay=1000, fixedRate=1000)
public void testScheduling() throws InterruptedException {
System.out.println("Started : "+ ++i);
Thread.sleep(4000);
System.out.println("Finished : "+ i);
}
輸出:
發起者:1
完成:1 // 4秒後
發起者:2 //立即W/O等待1秒中規定固定速率
表面處理:2 // 4秒後
等
「FIXEDDELAY」:等待來自先前執行的結束X米利斯開始下一次執行之前。無論當前執行的時間多少,下一次執行都是在添加「fixedDelay」間隔以結束當前執行時間之後開始的。它不會排隊下一次執行。
private static int i = 0;
@Scheduled(initialDelay=1000, fixedRate=1000)
public void testScheduling() throws InterruptedException {
System.out.println("Started : "+ ++i);
Thread.sleep(4000);
System.out.println("Finished : "+ i);
}
輸出:
發起者:1
完成:1 // 4秒後
發起者:1秒2個//等待如FIXEDDELAY指定
表面處理:2 4秒後
// 發起者:3 // 1秒後
等
在每個方法中我使用** Thread.sleep(5000)**來等待5s,但是我不知道看看有什麼不同 – Adam
完全一樣,我相信@ nikhil7610解釋正確 – ottercoder