zoukankan      html  css  js  c++  java
  • 定时任务调度工作(学习记录 四)schedule与scheduleAtFixedRate的区别

    根据两种情况来看区别

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

    1.schedule方法

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

    演示:

    public class DifferenceTest {
        public static void main(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 = new Timer();
            //第一次执行时间为6秒前,之后每隔两秒钟执行一次
            timer.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    // 打印当前的计划执行时间
                    System.out.println("Scheduled exec time is:" + 
                    sf.format(scheduledExecutionTime()));
                    System.out.println("Task is being executed!");        
                }            
            }, calendar.getTime(),2000);
            
        }
    }

    执行效果:

    2.scheduleAtFixedRate方法

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

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

    演示:

    public class DifferenceTest {
        public static void main(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 = new Timer();
            //第一次执行时间为6秒前,之后每隔两秒钟执行一次
            timer.scheduleAtFixedRate(new TimerTask() {
    
                @Override
                public void run() {
                    // 打印当前的计划执行时间
                    System.out.println("Scheduled exec time is:" + 
                    sf.format(scheduledExecutionTime()));
                    System.out.println("Task is being executed!");        
                }            
            }, calendar.getTime(),2000);
            
        }
    }

    执行效果如下:

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


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

    1.schedule方法

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

    演示:

    public class DifferenceTest {
        public static void main(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 = new Timer();
            //第一次执行时间为6秒前,之后每隔两秒钟执行一次
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    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);        
        }
    }

    执行结果:

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

    2.scheduleAtFixedRate方法

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

    演示:

    public class DifferenceTest {
        public static void main(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 = new Timer();
            //第一次执行时间为6秒前,之后每隔两秒钟执行一次
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    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);        
        }
    }

    执行结果如下:

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

  • 相关阅读:
    luoguP1558 色板游戏
    Tyvj1147
    Tyvj1147
    带修改的莫队(日常普及知识)
    带修改的莫队(日常普及知识)
    luoguP1903 数颜色(通过一道题认识带修改莫队)
    luoguP1903 数颜色(通过一道题认识带修改莫队)
    108.虚函数表原理(获取虚函数)
    104.virtual虚函数多态与异构数据结构
    106.多态与虚函数
  • 原文地址:https://www.cnblogs.com/xk920/p/10751505.html
Copyright © 2011-2022 走看看