首页编程scheduleatfixedrate(schedule和scheduleAtFixedRate的区别)

scheduleatfixedrate(schedule和scheduleAtFixedRate的区别)

编程之家2023-11-04157次浏览

其实scheduleatfixedrate的问题并不复杂,但是又很多的朋友都不太了解schedule和scheduleAtFixedRate的区别,因此呢,今天小编就来为大家分享scheduleatfixedrate的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

scheduleatfixedrate(schedule和scheduleAtFixedRate的区别)

...的scheduledexecutorservice.scheduleatfixedrate

java中的定时器功能

在jdk1.5之前,大家都用传统的定时器Timer来实现该功能

如,我们需要定制一个特殊方法,在程序首次载入时就执行,以后每隔一定的时间去执行那个方法

传统的做法如下;

scheduleatfixedrate(schedule和scheduleAtFixedRate的区别)

[html] view plain copy

/**

*定时器的测试(传统方式)

*/

public static void testTimer(){

scheduleatfixedrate(schedule和scheduleAtFixedRate的区别)

Timer timer= new Timer();

TimerTask task= new TimerTask(){

@Override

public void run(){

System.out.println("Timer:测试开始!");

}

};

//第一个参数是要执行的任务

//第二个是程序启动后要延迟多长后执行,单位毫秒

//第三个参数是,第一次执行后,以后每隔多长时间后在行

timer.schedule(task, 5000, 3000);

}

jdk1.5出来后,我们就可以改变这种做法,换种方式

如代码:

[html] view plain copy

/**

*定时器的测试(ScheduledExecutorService)

*/

public static void testExcuters(){

ScheduledExecutorService service= Executors.newScheduledThreadPool(1);

service.scheduleAtFixedRate(new Runnable(){

@Override

public void run(){

System.out.println("ScheduledExecutorService:测试开始");

}

}, 5, 3,TimeUnit.SECONDS);

}

scheduleAtFixedRate与scheduleWithFixedDelay区别

先来看字面意思:

1、scheduleAtFixedRate方法,顾名思义,它的方法名称的意思是:已固定的频率来执行某项计划(任务)。

2、scheduleWithFixedDealy,相对固定的延迟后,执行某项计划。

单从它们的字面来看,还是让人困惑,跟TC的管猿似的,文字游戏。

还是比较简单明了的描述比较好:第一个方法是固定的频率来执行某项计划,它不受计划执行时间的影响。到时间,它就执行。

而第二个方法,相对固定,据鄙人理解,是相对任务的。即无论某个任务执行多长时间,等执行完了,我再延迟指定的时间。也就是第二个方法,它受计划执行时间的影响。

不知道诸位看懂了没。

我再举一个简单的例子吧。

比方说:

某航空公司的航班:对于普通老百姓来说,不管你是在路上堵车了,还是正在跑呢,它是按时按点飞的。你的延误耽误不了飞机的起飞。这就是第一个方法。

我的理解:

     ScheduledExecutorServices中的cheduleAtFixedRate就是按点跑,也就是规定频率为1h,那么好,A任务开始执行,过来一个小时后,不管A是否执行完,都开启B任务;而scheduleWithFixedDealy却是需要在A任务执行完后,在经过1小时后再去执行B任务;

schedule和scheduleAtFixedRate的区别

scheduleAtFixedRate()与 schedule()的区别

scheduleAtFixedRate()与 schedule()看起来功能很像,其实不然。

scheduleAtFixedRate()的API说明有这样一段话:

If an execution is delayed for any reason(such as garbage collection or other background activity), two or more executions will occur in rapid succession to"catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period.

翻译过来的大概意思是:如果任务比较复杂,或者由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则scheduleAtFixedRate方法将快速连续地出现两次或更多的执行,从而使后续执行能够“追赶上来”。从长远看,执行的频率很接近指定的周期。

而schedule()的API说明是这样写的:

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason(such as garbage collection or other background activity), subsequent executions will be delayed as well.In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period.

翻译过来大概意思是:如果任务比较复杂,或者由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则接下来的任务也会被延迟,从长远看,schedule执行的频率将稍微比指定的周期要低点。

scheduleWithFixedDelay 和 scheduleAtFixedRate 的区别

ScheduledExecutorService#scheduleAtFixedRate()指的是“以固定的频率”执行,period(周期)指的是两次成功执行之间的时间

比如, scheduleAtFixedRate(command, 5, 2, second),第一次开始执行是5s后,假如执行耗时1s,那么下次开始执行是7s后,再下次开始执行是9s后

而ScheduledExecutorService#scheduleWithFixedDelay()指的是“以固定的延时”执行,delay(延时)指的是一次执行终止和下一次执行开始之间的延迟

scheduleWithFixedDelay(command, 5, 2, second),第一次开始执行是5s后,假如执行耗时1s,执行完成时间是6s后,那么下次开始执行是8s后,再下次开始执行是11s后

感谢您花时间阅读本文!我们希望通过对scheduleatfixedrate的问题进行探讨,为您提供了一些有用的见解和解决方案。如果您需要更多帮助或者有其他疑问,请不要犹豫与我们联系。

支持外链的网盘?哪些网盘可以支持外链功能呢webstorm license server(webstorm license在哪个菜单下)