清单 1. 使用 Timer 进行任务调度
package com.ibm.scheduler; import java.util.Timer; import java.util.TimerTask; public class TimerTest extends TimerTask { private String jobName=""; public TimerTest(String jobName){ super(); this.jobName=jobName; } @Override public void run() { // TODO Auto-generated method stub System.out.println("execute"+jobName); } public static void main(String[] args){ Timer timer=new Timer(); long delay1=1*1000; long period1=1000;
// 从现在开始 1 秒钟之后,每隔 1 秒钟执行一次 job1
timer.schedule(new TimerTest("job1"), delay1,period1);
long delay2=2*1000; long period2=2000;
// 从现在开始 2 秒钟之后,每隔 2 秒钟执行一次 job2
timer.schedule(new TimerTest("job2"), delay2,period2); } }
Output:
execute job1
execute job1
execute job2
execute job1
execute job1
execute job2
使用Timer实现任务调度的核心类是Timer和TimerTask。其中Timer负责设定TimerTask的起始与间隔执行时间。使用者只需要创建一个TimerTask的继承类,实现自己的run方法,然后将其丢给Timer去执行即可。
Timer的设计核心是一个TaskList和一个TaskThread。Timer将接收到任务丢到自己的TaskList中,TaskList按照Task的最初执行时间进行排序。TimerThread在创建Timer时会启动成为一个守护线程。这个线程会轮询所有任务,找到一个最近要执行的任务,然后休眠,当到达最近要执行任务的开始时间点,TimerThread被唤醒并执行该任务。之后TimerThread更新最近一个要执行的任务,继续休眠。
Timer的优点在于简单易用,但由于所有任务都是由通一个线程来调度,因此所有任务都是串行执行的,同一时间只能有一个任务在执行,前一个任务的延迟或异常都将会影响到之后的任务。
ScheduleExecutor
鉴于Timer的上述缺陷,JAVA5推出了基于线程池设计的ScheduleExecutor。其设计思想是,每一个被调度的任务都会由线程池中的一个线程去执行,因此任务是并发执行的,相互之间不会受到干扰。需要注意的是,只有当任务的执行时间到来时。ScheduleExecutor才会真正启动一个线程,其余时间ScheduleExecutor都是轮询任务的状态。
清单 2. 使用 ScheduledExecutor 进行任务调度
package com.ibm.scheduler; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ScheduledExecutorTest implements Runnable { private String jobName=""; public ScheduledExecutorTest(String jobName){ super(); this.jobName=jobName; } @Override public void run() { // TODO Auto-generated method stub System.out.println("execute"+jobName); } public static void main(String[] args){ ScheduledExecutorService service=Executors.newScheduledThreadPool(10); long initialDelay1=1; long period1=1; // 从现在开始1秒钟之后,每隔1秒钟执行一次job1 service.scheduleAtFixedRate( new ScheduledExecutorTest("job1"),initialDelay1,period1,TimeUnit.SECONDS); long initialDelay2=2; long delay2=2; // 从现在开始2秒钟之后,每隔2秒钟执行一次job2 service.scheduleWithFixedDelay(new ScheduledExecutorTest("job2"),initialDelay2,delay2,TimeUnit.SECONDS); } }
Output:
execute job1
execute job1
execute job2
execute job1
execute job1
execute job2
清单2展示了ScheduledExecutorService中两种最常见的调度方法ScheduleStFixedRate和ScheduleWithFixedDelay。ScheduleStFixedRate每次执行时间为上一次任务开始起向后推一个时间间隔,即每次执行时间为:initialDelay,initialDelay+period,initialDelay+2*period.....................................;
ScheduleWithFixedDelay每次执行时间为上一次任务结束起向后推一个时间间隔,即每次执行时间为:initialDelay,nitialDelay+executeTime+delay, initialDelay+2*executeTime+2*delay...................;由此可见,ScheduleStFixedRate是基于固定时间间隔进行任务调度,ScheduleWithFixedDelay 取决于每次任务执行的时间长短,是基于不固定时间间隔进行任务调度。
代码见附件
参考文献:
相关推荐
Android---Service Timer之执行周期任务
C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4...
Remanent On-delayTimer for S7-300400.zip西门子PLC编程实例程序源码下载Remanent On-delayTimer for S7-300400.zip西门子PLC编程实例程序源码下载Remanent On-delayTimer for S7-300400.zip西门子PLC编程实例程序...
AndroidTimer 一种简单的正计时和倒计时的Android 程序,精确的时间到毫秒 其中的ToggleButton是引用第三方库 TODO 添加开始、暂停、复位功能
12-2-1-定时器 Timer-相关例程演示 12-2-2-定时器 Timer-特性及时钟 12-2-3-定时器 Timer-模式
visual c++编写ActiveX OCX控件--Timer
TeaTimer BETA Author: Ralph Gootee ...Changelog in version 1.3: Pause Button Fixed a bug with the number picker dialog ...Stole the number picker dialog from astrid for android, the bes
STC8a8k所有定时器测试程序
29-DEMO-使用Timer0做16路软件PWM29-DEMO-使用Timer0做16路软件PWM
手把手教你写S12XS128程序(17)--Timer模块介绍1
stc8单片机 相关资料 范例:-使用Timer2模拟一个半双工串口-C和汇编,-使用Timer2模拟一个半双工串口-C和汇编的源程序,源程序,
Arduino-arduino-timer.zip,用于延迟函数调用的非阻塞库arduino timer-用于延迟函数调用的库,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,深受电子制造商的欢迎,Arduino通过开源系统提供了很多...
前端项目-d3-timer,一个高效的队列,能够管理数千个并发动画。
单片机源码学习参考-TIMER0与TIMER1控制条形LED.zip
经典的Android项目——jwood-standup-timer-.zip
反时间限制,让许多试用版软件没有时间限制
Engineer's Mini-Notebook - 555 Timer IC Circuits
LM3S例程系列,非常实用。 代码稍加修改即可使用。
02-Timer0-Timer1-Timer2测试程序_STC15W408AS跑马灯程序_源码.zip