schedule和scheduleAtFixedRate区别

schedule和scheduleAtFixedRate区别需求:由于系统长期运作,各设备之间产生很多信息,一段时间后需要清除数据考虑方案:用schedule还是scheduleAtFixedRate,在此比较分析了下这两个的区别schedule和scheduleAtFixedRate的区别在于,如果指定开始执行的时间在当前系统运行时间之前,schedule

schedule和scheduleAtFixedRate区别"

需求:

由于系统长期运作,各设备之间产生很多信息,一段时间后需要清除数据

考虑方案:

用schedule还是scheduleAtFixedRate,在此比较分析了下这两个的区别

schedule和scheduleAtFixedRate的区别在于,如果指定开始执行的时间在当前系统运行时间之前,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就定下来了。说白了,这个方法更注重保持执行频率的稳定。

package TimerMG;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/***
 * [Schedule]
 * @author Visec丶Dana
 * schedule方法:“fixed-delay”;
 * 如果第一次执行时间被delay了,随后的执行时间按 照 上一次 实际执行完成的时间点 进行计算
 */
public class ScheduleWay {
    public static void main(String[] args) throws ParseException {
        final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date startDate = dateFormatter.parse("2014-11-14 10:30:00");  
        Timer timer = new Timer();  
        timer.schedule(new TimerTask(){  
            public void run() {  
                try {   
                    Thread.sleep(2000);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();   
                }  
                System.out.println("execute task!  "+ dateFormatter.format(this.scheduledExecutionTime()));  
            }  
        },startDate, 5 * 1000);  
    }  
}
execute task!  2014-11-14 11:24:14
execute task!  2014-11-14 11:24:19
execute task!  2014-11-14 11:24:24
execute task!  2014-11-14 11:24:29
ScheduleAtFixed
package TimerMG;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
 * [ScheduleAtFixed]
 * @author Visec丶Dana
 * fixed-rate;如果第一次执行时间被delay了,
 * 随后的执行时间按照 上一次开始的 时间点 进行计算,
 * 并且为了”catch up”会多次执行任务,TimerTask中的执行体需要考虑同步
 */
public class ScheduleAtFixedRateWay{
    public static void main(String[] args) throws ParseException {
        final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date startDate = dateFormatter.parse("2014-11-14 10:30:00");  
        Timer timer = new Timer();   
        timer.scheduleAtFixedRate(new TimerTask(){  
           public void run()  
           {   
               try {   
                    Thread.sleep(2000);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
               System.out.println("execute task!  " + dateFormatter.format(this.scheduledExecutionTime()));  
           }  
        },startDate,5*1000);  
    }
}
execute task!  2014-11-14 10:30:00
execute task!  2014-11-14 10:30:05
execute task!  2014-11-14 10:30:10
execute task!  2014-11-14 10:30:15
execute task!  2014-11-14 10:30:20

今天的文章schedule和scheduleAtFixedRate区别分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/50066.html

(0)
编程小号编程小号
上一篇 2023-09-02 15:11
下一篇 2023-09-02

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注