理解 Spring 定時任務的 fixedRate 和 fixedDelay 的區別

用過  Spring 的 @EnableScheduling 的都知道,咱們用三種形式來部署計劃任務,即 @Scheduled 註解的 fixedRate(fixedRateString), fixedDelay(fixedDelayString), 以及 cron. cron 不在這裏討論的範疇。咱們着重在如何理解 fixedRate 和 fixedDelay 的區別。html

在 Spring 的  Scheduled 註解的 JavaDoc 對此的解釋很簡單java

public abstract long fixedRate 
Execute the annotated method with a fixed period in milliseconds between invocations.spring

public abstract long fixedDelay 
Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.api

只是說是 fixedRate 任務兩次執行時間間隔是任務的開始點,而 fixedDelay 的間隔是前次任務的結束與下次任務的開始。htm

大體用示意字符串來表示以下(每一個 T1, 或 T2 表明任務執行秒數(每次任務執行時間不定),假定 fixedRate 或  fixedDelay 的值是 5 秒,用 W 表示等待的數)字符串

fixedRate:    T1.T1WWWT2.T2.T2WW.T3.T3.T3.T3.T3.T4.T4.T4.T4.T4.T4.T4T5T5WWWT6.T6........部署

fixedDelay:  T1.T1.WWWWW.T2.T2.T2WWWWW.T3.T3.T3.T3.T3.WWWWW.T4.T4.T4.T4.T4.T4.T4.WWWWWT6.T6......get

通常來講能理解到上面兩個場景已經差很少了,相比而言 fixedDelay 簡單些,盯着上一次任務的屁股就行。 閱讀全文 >>it

相關文章
相關標籤/搜索