理解 Spring 定时任务的 fixedRate 和 fixedDelay 的区别
2017-09-07 16:08
323 查看
用过 Spring 的 @EnableScheduling 的都知道,我们用三种形式来部署计划任务,即 @Scheduled 注解的 fixedRate(fixedRateString), fixedDelay(fixedDelayString), 以及 cron. cron 不在这里讨论的范畴。我们着重在如何理解 fixedRate 和 fixedDelay 的区别。
在 Spring 的 Scheduled 注解的
JavaDoc 对此的解释很简单
public abstract long fixedRate
Execute the annotated method with a fixed period in milliseconds between invocations.
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.
只是说是 fixedRate 任务两次执行时间间隔是任务的开始点,而 fixedDelay 的间隔是前次任务的结束与下次任务的开始。
大致用示意字符串来表示如下(每个 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......
一般来说能理解到上面两个场景已经差不多了,相比而言 fixedDelay 简单些,盯着上一次任务的屁股就行。 阅读全文
>>
在 Spring 的 Scheduled 注解的
JavaDoc 对此的解释很简单
public abstract long fixedRate
Execute the annotated method with a fixed period in milliseconds between invocations.
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.
只是说是 fixedRate 任务两次执行时间间隔是任务的开始点,而 fixedDelay 的间隔是前次任务的结束与下次任务的开始。
大致用示意字符串来表示如下(每个 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......
一般来说能理解到上面两个场景已经差不多了,相比而言 fixedDelay 简单些,盯着上一次任务的屁股就行。 阅读全文
>>
相关文章推荐
- spring定时器使用注解@Scheduled执行任务,fixedDelay、fixedRate和cron区别
- Spring @Scheduled定时任务的fixedRate,fixedDelay,cron的作用和不同
- spring中使用@Scheduled注解时corn表达式、fixedRate、fixedDelay的区别
- Spring @Scheduled定时任务的fixedRate,fixedDelay,cron执行差异
- ScheduleExecutorService接口和spring控制定时任务的理解
- 使用spring @Scheduled注解执行定时任务
- spring 定时任务执行两次
- Spring定时任务有时候会莫名奇妙的终止?
- spring MVC 配置定时任务任务
- spring定时任务详解(@Scheduled注解)
- Spring中添加定时任务 JOB
- spring @Scheduled 实现定时任务的注意事项
- Spring Quartz定时任务不准时执行
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)(转的)
- spring 定时任务cron在线表达式生成
- Spring Boot 定时任务使用中的鸡肋
- Spring定时任务的几种实现
- 详解Spring Boot 定时任务的实现方法
- Spring+Quartz 从数据库中获取定时任务和定时时间,动态实现对定时任务的增删改查
- Spring + Quartz 轻松搞定web任务定时调度