schedule和scheduleAtFixedRate区别

来源:互联网 发布:河南卫生网络直报系统 编辑:程序博客网 时间:2024/06/05 05:31

schedule和scheduleAtFixedRate的区别在于,如果指定开始执行的时间在当前系统运行时间之前,scheduleAtFixedRate会把已经过去的时间也作为周期执行,而schedule不会把过去的时间算上。

比如

SimpleDateFormat fTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d1 = fTime.parse("2005/12/30 14:10:00");

t.scheduleAtFixedRate(new TimerTask(){
public void run()
{
System.out.println("this is task you do6");
}
},d1,3*60*1000);

间隔时间是3分钟,指定开始时间是2005/12/30 14:10:00,如果我在14:17:00分执行这个程序,那么会立刻打印3次

this is task you do6 //14:10
this is task you do6 //14:13
this is task you do6 //14:16

并且注意,下一次执行是在14:19 而不是 14:20。就是说是从指定的开始时间开始计时,而不是从执行时间开始计时。

但是上面如果用schedule方法,间隔时间是3分钟,指定开始时间是2005/12/30 14:10:00,那么在14:17:00分执行这个程序,则立即执行程序一次。并且下一次的执行时间是 14:20,而不是从14:10开始算的周期(14:19)。

 

需要注意的是scheduleAtFixedRate和schedule在参数完全相同的情况下,执行效果是不同的。上面例子中任务只是打印一个字符串,比较简单。但如果任务比较复杂,或者由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则scheduleAtFixedRate方法将快速连续地出现两次或更多的执行,从而使后续执行能够“追赶上来”;而schedule方法的后续执行也将被延迟。所以,在长期运行中,scheduleAtFixedRate执行的频率将正好是指定周期的倒数,schedule 执行的频率一般要稍慢于指定周期的倒数。

 

==================================================================================================

schedule和scheduleAtFixedRate 区别:

(1) 2个参数的schedule在制定任务计划时, 如果指定的计划执行时间scheduledExecutionTime<= systemCurrentTime,则task会被立即执行。scheduledExecutionTime不会因为某一个task的过度执行而改变。
(2) 3个参数的schedule在制定反复执行一个task的计划时,每一次执行这个task的计划执行时间随着前一次的实际执行时间而变,也就是 scheduledExecutionTime(第n+1次)=realExecutionTime(第n次)+periodTime。也就是说如果第n 次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),则此时不做时隔等待,立即执行第n+1次task,而接下来的第n+2次task的 scheduledExecutionTime(第n+2次)就随着变成了realExecutionTime(第n+1次)+periodTime。说 白了,这个方法更注重保持间隔时间的稳定。
(3)3个参数的scheduleAtFixedRate在制定反复执行一个task的计划时,每一次 执行这个task的计划执行时间在最初就被定下来了,也就是scheduledExecutionTime(第n次)=firstExecuteTime +n*periodTime;如果第n次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),则此时不做period间隔等待,立即执行第n+1次task,而接下来的第n+2次的 task的scheduledExecutionTime(第n+2次)依然还是firstExecuteTime+(n+2)*periodTime这 在第一次执行task就定下来了。说白了,这个方法更注重保持执行频率的稳定。

Timer的实例:

package com.hemes.timer;

import java.util.*;

public class doTask extends TimerTask {

// true时使用后台进程线程。只要剩下的程序记叙运行,后台进程线程就会执行。
Timer myTimer;

public void start(int delay, int hour) {
myTimer = new Timer();
myTimer.schedule(this, delay * 1000, hour*1000*60*60); //利用timer.schedule方法

//public void schedule(TimerTask task,long time,long period)
//task被安排在延迟time后执行,执行后将每隔period(毫秒)反复执行。由于规定的时间间隔并不能保证与时钟精准的同不步,所以该方
}

public void start(Date time, int hour) {
myTimer = new Timer();
myTimer.schedule(this, time, hour*1000*60*60); //利用timer.schedule方法

//public void schedule(TimerTask task,Date time,long period)
//task被安排在time指定的时间执行,执行后将每隔period(毫秒)反复执行。由于规定的时间间隔并不能保证与时钟精准的同不步,所以该方
}

public void run() {
//执行任务(sql)
System.out.println("do Task...");
}

public void end(){
myTimer.cancel();
//终止Timer的功能执行,但不会对正在执行的任务有影响。当执行cancel方法后将不能再用其分配任务。
}

//测试
public static void main(String args[]) {

doTask myTask1 = new doTask();

//Get the Date corresponding to 11:30:00 pm today.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime();

myTask1.start(time,24);



//myTask1.end();//线程结束

}
}

0 0