• schedule() 和 scheduleAtFixedRate() 的区别--转载


    1.  schedule() ,2个参数方法:
    在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行。

    2.  schedule() ,3个参数方法:
    在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行,之后按period参数固定重复执行。

    3.  scheduleAtFixedRate() ,3个参数方法:
    在执行任务时,如果指定的计划执行时间scheduledExecutionTime<= systemCurrentTime,则task会首先按执行一次;然后按照执行时间、系统当前时间和period参数计算出过期该执行的次数,计算按照: (systemCurrentTime-scheduledExecutionTime)/period,再次执行计算出的次数;最后按period参数固定重复执行。

    4.  schedule() 和scheduleAtFixedRate() 
    schedule()方法更注重保持间隔时间的稳定。
    scheduleAtFixedRate()方法更注重保持执行频率的稳定。

    package com.task;
    
    import java.util.Date;
    import java.util.Timer;
    
    public class TestTask {
    
     /**
      * @param args
      */
     public static void main(String[] args){
      Date crtTime = new Date();
      long crt = crtTime.getTime();
      Timer timer = new Timer();
      
      // 在指定时间执行
      CommonTask task1 = new CommonTask("【任务一】"); 
      timer.schedule(task1, new Date(crt - 1000));
      
      // schedule和scheduleAtFixedRate 
      CommonTask task11 = new CommonTask("【任务二】"); 
      timer.schedule(task11, new Date(crt - 10 * 1000),1000);
      
      CommonTask task12 = new CommonTask("【任务三】"); 
      timer.schedule(task12, new Date(crt - 10 * 1000),1000);
      
      // schedule和scheduleAtFixedRate 
      CommonTask task13 = new CommonTask("【任务四】"); 
      timer.schedule(task13, new Date(crt + 2 * 1000),1000);
      
      CommonTask task14 = new CommonTask("【任务五】"); 
      timer.scheduleAtFixedRate(task14, new Date(crt + 2 * 1000),1000);
     }
    
    }

    原文地址:http://blog.163.com/nice_2012/blog/static/192666148201231635332934/

  • 相关阅读:
    SqlServer CDC的使用
    javabean的自省机制
    javascript验证汉字,字符,等正则表达式
    温习ASP调用C#的DLL并实现用户名密码进行域验证反馈结果
    基于RSA算法在asp中加密与解密对应的函数
    SQL查询所有表,字段名,主键,类型,长度,小数位数,允许空,默认值,字段说明
    c语言文法定义
    0909对编译原理的了解
    语法分析
    1112评论
  • 原文地址:https://www.cnblogs.com/davidwang456/p/4290732.html
Copyright © 2020-2023  润新知