用过 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.springpublic 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