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);        
        }
    }

    执行结果如下:

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

  • 相关阅读:
    Manjaro 安装 VMware Pro 15
    RAID-0-10 搭建和使用
    列表的切换按钮,是什么实现的?
    动态表单的设计
    如何修改自定义表单的名字,不适用diyname,直接使用id
    php关于批量替换的测试
    fastadmin删除控制器,删除菜单,提示not found
    fastadmin自定义表单,如何根据字段信息,创建表,而且,可以随时修改字段的顺序
    fastadmin如何获取新增后的id,这个可以使用模型的钩子函数。
    fastadmin如何在弹窗内跳转?以及如何在非弹窗页面,做tab选项卡
  • 原文地址:https://www.cnblogs.com/xk920/p/10751505.html
Copyright © 2011-2022 走看看