zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    EditPlus使用技巧
    PL/SQL Dev的问题
    解决httpModules 未能从程序集 XX 加载类型 XXX 的错误
    IE浏览器无法显示背景,字体显示很大问题的解决办法[转]
    如何在Outlook2003中加入农历节气
    再谈Oracle在Windows下的权限问题
    Vista下安装布署注册的问题解决
    [转]关于管理的经典故事(员工激励)
    开始应用AJAX
    Aptana IDE 中文乱码的问题解决
  • 原文地址:https://www.cnblogs.com/visec479/p/4096880.html
Copyright © 2011-2022 走看看