ScheduledExecutorService 定时任务,scheduleAtFixedRate和scheduleWithFixedDelay区别

比Timer用着方便,用过很多次,经常被用

scheduleWithFixedDelay,每一次以固定的延迟时间开始调度task

参数
1 command the task to execute
2 initialDelay the time to delay first execution
3 delay the delay between the termination of one execution  and the commencement of the next
4 unit the time unit of the initialDelay and delay parameters

解释
第一次调度开始时间点=当前时间+initialDelay 
下一次调度开始时间点=上一次task完成时间点+delay

scheduleAtFixedRate,每一次以固定的频率开始调度task

这是参数
1 command the task to execute
2 initialDelay the time to delay first execution
3 period the period between successive executions
4 unit the time unit of the initialDelay and period parameters

解释
第一次调度开始时间点=当前时间+initialDelay
第二次调度开始时间点=initialDelay + perid
第n次调度开始时间点=initialDelay + n *perid

比如:
现在是12:00启动服务 设定initialDelay=1;period=2;unit=小时
第一次:12:00 + 1 =13:00
第二次:12:00 + 1 +2 =15:00
第三次次:12:00 +1 + 2 + 2 =17:00
...
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值