java schedule间隔 0_定时任务调度工作(学习记录 四)schedule与scheduleAtFixedRate的区别...

c9469a545170fc53e6417b8b1a6c3d91.png

根据两种情况来看区别

一.首次计划执行的时间早于当前的时间

1.schedule方法

“fixed-delay”:如果第一次执行时间被延迟了,随后的执行时间按照上一次实际执行完成的时间点进行计算

演示:

public classDifferenceTest {public static voidmain(String[] args) {//规定时间格式

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取当前的具体时间

Calendar calendar =Calendar.getInstance();

System.out.println("Current time is:" +sf.format(calendar.getTime()));//设置成6秒前的时间,若当前时间为2019-04-22 15:44:50//那么设置之后的时间变成2019-04-22 15:44:44

calendar.add(Calendar.SECOND, -6);

Timer timer= newTimer();//第一次执行时间为6秒前,之后每隔两秒钟执行一次

timer.schedule(newTimerTask() {

@Overridepublic voidrun() {//打印当前的计划执行时间

System.out.println("Scheduled exec time is:" +sf.format(scheduledExecutionTime()));

System.out.println("Task is being executed!");

}

}, calendar.getTime(),2000);

}

}

执行效果:

dd1e1f91faaeae147f02ebfbf06dfb86.png

2.scheduleAtFixedRate方法

“fixed-rate”;如果第一次执行时间被延迟了,随后的执行时间按照上一次开始的时间点进行计算,

并且为了赶上进度会多次执行任务,因此TimerTask中的执行体需要考虑同步。

演示:

public classDifferenceTest {public static voidmain(String[] args) {//规定时间格式

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取当前的具体时间

Calendar calendar =Calendar.getInstance();

System.out.println("Current time is:" +sf.format(calendar.getTime()));//设置成6秒前的时间,若当前时间为2019-04-22 15:44:50//那么设置之后的时间变成2019-04-22 15:44:44

calendar.add(Calendar.SECOND, -6);

Timer timer= newTimer();//第一次执行时间为6秒前,之后每隔两秒钟执行一次

timer.scheduleAtFixedRate(newTimerTask() {

@Overridepublic voidrun() {//打印当前的计划执行时间

System.out.println("Scheduled exec time is:" +sf.format(scheduledExecutionTime()));

System.out.println("Task is being executed!");

}

}, calendar.getTime(),2000);

}

}

执行效果如下:

03ea881626fe733537e0a73632cf814e.png

如图,因为设置了每隔2s执行一次,第一次执行时间比当前时间提早了6s,所以它会从原定最早的时间,先直接执行三次,来追上现在的进度。

二.任务执行时间超出执行周期间隔

1.schedule方法

下一次执行时间相对于上一次实际执行完成的时间点,因此执行时间会不断延后。

演示:

public classDifferenceTest {public static voidmain(String[] args) {//规定时间格式

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取当前的具体时间

Calendar calendar =Calendar.getInstance();

System.out.println("Current time is:" +sf.format(calendar.getTime()));

Timer timer= newTimer();//第一次执行时间为6秒前,之后每隔两秒钟执行一次

timer.schedule(newTimerTask() {

@Overridepublic voidrun() {try{

Thread.sleep(3000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//打印当前的计划执行时间

System.out.println("Scheduled exec time is:" +sf.format(scheduledExecutionTime()));

System.out.println("Task executes!");

}

}, calendar.getTime(),2000);

}

}

执行结果:

05e860e936b57c6e6902676c337d63df.png

用sleep,模拟执行任务时间为三秒,大于任务间隔时间。

2.scheduleAtFixedRate方法

下一次执行时间相对于上一次开始的时间点,因此执行时间一般不会延后,因此存在并发性。

演示:

public classDifferenceTest {public static voidmain(String[] args) {//规定时间格式

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取当前的具体时间

Calendar calendar =Calendar.getInstance();

System.out.println("Current time is:" +sf.format(calendar.getTime()));

Timer timer= newTimer();//第一次执行时间为6秒前,之后每隔两秒钟执行一次

timer.scheduleAtFixedRate(newTimerTask() {

@Overridepublic voidrun() {try{

Thread.sleep(3000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//打印当前的计划执行时间

System.out.println("Scheduled exec time is:" +sf.format(scheduledExecutionTime()));

System.out.println("Task executes!");

}

}, calendar.getTime(),2000);

}

}

执行结果如下:

e4b8624159eb1cbac23f52a26fbdac30.png

不会被任务执行所需要时间影响。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值