含雪幸福

导航

 

一。定义

  有且仅有一个后台线程多个业务线程进行定时定频率的调度

二。

  Timer  ---->  Timer Task  (中有run();方法)

  通过 new Timer().schedule() 来调用 Timer Taskrun()方法

  TimerTask是一个抽象类,它的子类由 Timer 安排为一次执行或重复执行的任务。实际上就是一个拥有run方法的类

需要定时执行的代码放到run方法体内

Timer 是Java中util包中的方法

  

 1 Timer timer = new Timer();   
 2 // 注意javax.swing包中也有一个Timer类,如果import中用到swing包,要注意名字的冲突。   
 3 
 4 TimerTask task = new TimerTask() {   
 5     public void run() {   
 6         ... //每次需要执行的代码放到这里面。   
 7     }   
 8 };   
 9 
10 //以下是几种常用调度task的方法:     
11    timer.schedule(task, time);   
12    // time为Date类型:在指定时间执行一次(不周期)。   
13 
14    timer.schedule(task, firstTime, period);   
15    // firstTime为Date类型,period为long   
16    // 从firstTime时刻开始,每隔period毫秒执行一次。   
17 
18    timer.schedule(task, delay) // delay 为long类型:从现在起过delay毫秒之后执行一次(不周期)
19 --------------------------------------------------------
20 
21    timer.schedule(task, delay, period)   
22    // delay为long,period为long:从现在起过delay毫秒以后,每隔period   
23    // 毫秒执行一次。
   // 例如:从运行当前时间开始后的 2秒后开始执行,每1秒调用一次
    timer.schedule(task, 2000L, 1000L)  ; 
 

说明::  timer.schedule(new MyTask(),long time1,long timer2);

    第一个参数,是 TimerTask 类,在包:import java.util.TimerTask .使用者要继承该类,并实现public void run() 方法,因为 TimerTask 类 实现了 Runnable 接口。

    第二个参数的意思是,当你调用该方法后,该方法必然会调用 TimerTask 类 TimerTask 类 中的 run()方法,这个参数就是这两者之间的差值,转换成汉语的意思就是说,用户调用 schedule() 方法后,要等待这么长的时间才可以第一次执行run() 方法。

    第三个参数的意思就是,第一次调用之后,从第二次开始每隔多长的时间调用一次 run() 方法。

[附:]

  技术人员在实现内部办公系统与外部网站一体化的时候,最重要的步骤就是从OA系统读取数据,并且根据网站模板生成最终的静态页面。这里就需要一个定时任务,循环的执行。

  技术人员在写定时任务的时候,想当然的以为Timer.schedule(TimerTask task, longdelay)就是重复的执行task。程序运行后发现只运行了一次,总觉得是task里的代码有问题,花了很长时间调试代码都没有结果。

  仔细研读java api,发现:

  schedule(TimerTask task, long delay)的注释:Schedules thespecified task for execution after the specifieddelay。大意是在延时delay毫秒后执行task。并没有提到重复执行

  schedule(TimerTask task, long delay, long period)的注释:Schedulesthe specified task for repeated fixed-delay execution, beginningafter the specified delay。大意是在延时delay毫秒后重复的执行task,周期是period毫秒。

  这样问题就很明确schedule(TimerTask task, longdelay)只执行一次,schedule(TimerTask task, long delay, longperiod)才是重复的执行。关键的问题在于程序员误以为schedule就是重复的执行,而没有仔细的研究API,一方面也是英文能力不够,浏览API的过程中不能很快的理解到含义。

  

 

posted on 2018-02-24 11:06  含雪幸福  阅读(158)  评论(0编辑  收藏  举报
 
/*生成博客目录的CSS*/ #uprightsideBar{ font-size:12px; font-family:Arial, Helvetica, sans-serif; text-align:left; position:fixed;/*将div的位置固定到距离top:50px,right:0px的位置,这样div就会处在最右边的位置,距离顶部50px*/ top:50px; right:0px; width: auto; height: auto; } #sideBarTab{ float:left; width:30px; border:1px solid #e5e5e5; border-right:none; text-align:center; background:#ffffff; } #sideBarContents{ float:left; overflow:auto; overflow-x:hidden;!important; width:200px; min-height:108px; max-height:460px; border:1px solid #e5e5e5; border-right:none; background:#ffffff; } #sideBarContents dl{ margin:0; padding:0; } #sideBarContents dt{ margin-top:5px; margin-left:5px; } #sideBarContents dd, dt { cursor: pointer; } #sideBarContents dd:hover, dt:hover { color:#A7995A; } #sideBarContents dd{ margin-left:20px; }