schedule
public void schedule(TimerTask task,
Date firstTime,
long period)
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period. 就是说第一次从firstTime开始执行。
后续执行以period为时间间隔重复执行,即从firsttime开始,每period执行一次.
public void schedule(TimerTask task,
Date firstTime,
long period)
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period. 就是说第一次从firstTime开始执行。
后续执行以period为时间间隔重复执行,即从firsttime开始,每period执行一次.
public void processEvent();
} public Class Clock implements TimerListener{
Clock(){
Timer t=new Timer(this); //向Timer类登记
}
public void processEvent(){
//你的事件处理的代码
}
} class Timer extends Thread { private TimerListener tl;
Timer(TimerListener tl){
this.tl=tl;
}
public void run(){
while(true){
sleep(1000);
tl.processEvent();
}
}
}
第一次:2002/12/26/8:30
第二次:2002/12/26/8:40 + 你的任务运行的时间如果你想定时执行,而不是定间隔那么改用Timer.scheduleAtFixRate
第一次:TimerTask启动的时间
第二次:第一次+period(TimerTask执行时间<=period)或第一个TimerTask完成时(TimerTask执行时间>period)如果用Timer.scheduleAtFixRate就可以定时执行