Java语言中Timer类的简洁用法

所有类型的 Java 应用程序一般都需要计划重复执行的任务。企业应用程序需要计划每日的日志或者晚间批处理过程。一个 J2SE 或者 J2ME 日历应用程序需要根据用户的约定计划闹铃时间。不过,标准的调度类 Timer 和 TimerTask 没有足够的灵活性,无法支持通常需要的计划任务类型。在本文中,Java 开发人员 Tom White 向您展示了如何构建一个简单通用的计划框架,以用于执行任意复杂的计划任务。


  把 java.util.Timer 和 java.util.TimerTask 统称为 Java 计时器框架,它们使程序员可以很容易地计划简单的任务(注意这些类也可用于 J2ME 中)。在 Java 2 SDK, Standard Edition, Version 1.3 中引入这个框架之前,开发人员必须编写自己的调度程序,这需要花费很大精力来处理线程和复杂的 Object.wait() 方法。不过,Java 计时器框架没有足够的能力来满足许多应用程序的计划要求。甚至一项需要在每天同一时间重复执行的任务,也不能直接使用 Timer 来计划,因为在夏令时开始和结束时会出现时间跳跃。


  本文展示了一个通用的 Timer 和 TimerTask 计划框架,从而允许更灵活的计划任务。这个框架非常简单 ?? 它包括两个类和一个接口 ?? 并且容易掌握。如果您习惯于使用 Java 定时器框架,那么您应该可以很快地掌握这个计划框架。


  计划单次任务


  计划框架建立在 Java 定时器框架类的基础之上。因此,在解释如何使用计划框架以及如何实现它之前,我们将首先看看如何用这些类进行计划。


  想像一个煮蛋计时器,在数分钟之后(这时蛋煮好了)它会发出声音提醒您。清单 1 中的代码构成了一个简单的煮蛋计时器的基本结构,它用 Java 语言编写:

清单 1. EggTimer 类

package org.tiling.scheduling.examples;
import java.util.Timer;
import java.util.TimerTask;
public class EggTimer {
private final Timer timer = new Timer();
private final int minutes;
public EggTimer(int minutes) {
this.minutes = minutes;
}
public void start() {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}
private void playSound() {
System.out.println("Your egg is ready!");
// Start a new thread to play a sound...
}
}, minutes * 60 * 1000);
}
public static void main(String[] args) {
EggTimer eggTimer = new EggTimer(2);
eggTimer.start();
}
}


  EggTimer 实例拥有一个 Timer 实例,用于提供必要的计划。用 start() 方法启动煮蛋计时器后,它就计划了一个 TimerTask,在指定的分钟数之后执行。时间到了,Timer 就在后台调用 TimerTask 的 start() 方法,这会使它发出声音。在取消计时器后这个应用程序就会中止。

http://t.iqiyi.com/m/1017413/1405160333642468/review
http://t.iqiyi.com/m/1140628/140516033465237/review
http://t.iqiyi.com/m/1140628/14051603198856/review
http://t.iqiyi.com/m/1016856/140516039069736/review
http://t.iqiyi.com/m/1006255/14051603165223800/review
http://t.iqiyi.com/m/1150392/140516039664560/review
http://t.iqiyi.com/m/1149237/1405160339772425/review
http://t.iqiyi.com/m/1026291/1405160314095701/review
http://t.iqiyi.com/m/1026891/140516037101800/review
http://t.iqiyi.com/m/1023516/1405160339373555/review
http://t.iqiyi.com/m/1016776/1405160365150400/review
http://t.iqiyi.com/m/1016717/1405160315165900/review
http://t.iqiyi.com/m/1150379/1405160353628450/review
http://t.iqiyi.com/m/1023184/1405160313486175/review
http://t.iqiyi.com/m/1020144/1405160326527952/review
http://t.iqiyi.com/m/1020144/14051603607173/review
http://t.iqiyi.com/m/1020144/14051603247688000/review
http://t.iqiyi.com/m/1023601/140516031863539/review
http://t.iqiyi.com/m/1023601/14051603128008320/review
http://t.iqiyi.com/m/1017327/1405160312791808/review
http://t.iqiyi.com/m/1019902/1405160341853000/review
http://t.iqiyi.com/m/1019902/140516039369720/review
http://t.iqiyi.com/m/1019902/1405160316318719/review
http://t.iqiyi.com/m/1017024/140516031674816/review
http://t.iqiyi.com/m/1017024/1405160382471970/review
http://t.iqiyi.com/m/1019458/1405160311202210/review
http://t.iqiyi.com/m/1019775/1405160353718624/review
http://t.iqiyi.com/m/1022343/140516033887487/review
http://t.iqiyi.com/m/1022343/1405160315462117/review
http://t.iqiyi.com/m/1017397/1405160342154806/review
http://t.iqiyi.com/m/1022319/14051603244701000/review
http://t.iqiyi.com/m/1022319/1405160319090440/review
http://t.iqiyi.com/m/1019274/1405160318914280/review
http://t.iqiyi.com/m/1016854/1405160344551919/review
http://t.iqiyi.com/m/1018861/1405160312243000/review
http://t.iqiyi.com/m/1016766/140516039767184/review
http://t.iqiyi.com/m/1023064/14051603726950/review
http://t.iqiyi.com/m/1017272/1405160363295200/review
http://t.iqiyi.com/m/1017272/1405160320292048/review
http://t.iqiyi.com/m/1023243/1405160313943020/review
http://t.iqiyi.com/m/1020189/1405160310671318/review
http://t.iqiyi.com/m/1020189/1405160343049880/review
http://t.iqiyi.com/m/1020189/14051603109873200/review
http://t.iqiyi.com/m/1020189/1405160311009999/review
http://t.iqiyi.com/m/1020189/140516031119195/review
http://t.iqiyi.com/m/1020189/140516033157506/review
http://t.iqiyi.com/m/1020189/1405160318761820/review
http://t.iqiyi.com/m/1020189/14051603198016/review
http://t.iqiyi.com/m/1020189/1405160317794140/review
http://t.iqiyi.com/m/1020189/140516035572800/review
http://t.iqiyi.com/m/1023057/140516032199054/review
http://t.iqiyi.com/m/1023057/14051603598689/review
http://t.iqiyi.com/m/1023057/1405160326065165/review
http://t.iqiyi.com/m/1023057/1405160319625634/review
http://t.iqiyi.com/m/1023057/140516032097200/review
http://t.iqiyi.com/m/1023057/140516039309710/review
http://t.iqiyi.com/m/1023057/140516037672500/review
http://t.iqiyi.com/m/1023057/1405160321120/review
http://t.iqiyi.com/m/1023057/1405160318362221/review
http://t.iqiyi.com/m/1023057/1405160325687200/review
http://t.iqiyi.com/m/1023057/14051603741303/review
http://t.iqiyi.com/m/1023057/140516033396540/review
http://t.iqiyi.com/m/1023057/140516031954540/review
http://t.iqiyi.com/m/1023057/14051603416300/review
http://t.iqiyi.com/m/1023057/1405160314199680/review
http://t.iqiyi.com/m/1023057/140516033692640/review
http://t.iqiyi.com/m/1023057/1405160365707200/review
http://t.iqiyi.com/m/1023057/1405160310693375/review
http://t.iqiyi.com/m/1148494/140516037037951/review
http://t.iqiyi.com/m/1148494/140516031421584/review
http://t.iqiyi.com/m/1020854/140516031176672/review
http://t.iqiyi.com/m/1020854/140516039933143/review
http://t.iqiyi.com/m/1017314/1405160331077376/review
http://t.iqiyi.com/m/1017314/1405160322158360/review
http://t.iqiyi.com/m/1025084/14051603427158400/review
http://t.iqiyi.com/m/1025084/1405160315164448/review
http://t.iqiyi.com/m/1025084/1405160330634344/review
http://t.iqiyi.com/m/1025084/140516031892773/review
http://t.iqiyi.com/m/1025084/1405160326164920/review
http://t.iqiyi.com/m/1025084/1405160317905104/review
http://t.iqiyi.com/m/1149237/140516032047709/review
http://t.iqiyi.com/m/1019976/140516036269761/review
http://t.iqiyi.com/m/1022603/1405160313769375/review
http://t.iqiyi.com/m/1022603/140516031771200/review
http://t.iqiyi.com/m/1017481/14051603126882000/review
http://t.iqiyi.com/m/1017481/1405160349871640/review
http://t.iqiyi.com/m/1017481/14051603623574/review
http://t.iqiyi.com/m/1017481/1405160312048369/review
http://t.iqiyi.com/m/1017481/1405160339148350/review
http://t.iqiyi.com/m/1017481/140516031320100/review
http://t.iqiyi.com/m/1017481/140516035175640/review
http://t.iqiyi.com/m/1017481/1405160316438656/review
http://t.iqiyi.com/m/1016880/1405160347706780/review
http://t.iqiyi.com/m/1016880/140516037032648/review
http://t.iqiyi.com/m/1016880/1405160335017500/review
http://t.iqiyi.com/m/1016880/1405160349160160/review
http://t.iqiyi.com/m/1016880/140516039280464/review
http://t.iqiyi.com/m/1016880/1405160324162516/review
http://t.iqiyi.com/m/1016880/14051603499086000/review
http://t.iqiyi.com/m/1016880/140516036842115/review
http://t.iqiyi.com/m/1016880/140516035239775/review
http://t.iqiyi.com/m/1016698/140516032539767/review
http://t.iqiyi.com/m/1016698/1405160323148534/review
http://t.iqiyi.com/m/1016698/1405160311859708/review
http://t.iqiyi.com/m/1016698/1405160333288/review
http://t.iqiyi.com/m/1016698/1405160316290372/review
http://t.iqiyi.com/m/1021353/1405160320351562/review
http://t.iqiyi.com/m/1020446/1405160315640800/review
http://t.iqiyi.com/m/1022778/140516033235176/review
http://t.iqiyi.com/m/1022778/140516032355525/review
http://t.iqiyi.com/m/1022778/140516032237440/review
http://t.iqiyi.com/m/1022778/140516032433652/review
http://t.iqiyi.com/m/1022778/1405160318589925/review
http://t.iqiyi.com/m/1002181/140516034800972/review
http://t.iqiyi.com/m/1002181/140516038645282/review
http://t.iqiyi.com/m/1007881/1405160316836092/review
http://t.iqiyi.com/m/1007881/1405160317720220/review
http://t.iqiyi.com/m/1007881/14051603128439/review
http://t.iqiyi.com/m/1007881/1405160313612320/review
http://t.iqiyi.com/m/1007881/140516036089660/review
http://t.iqiyi.com/m/1007881/14051603326808/review
http://t.iqiyi.com/m/1007881/14051603701550/review
http://t.iqiyi.com/m/1007881/1405160331694400/review
http://t.iqiyi.com/m/1007881/140516035406912/review
http://t.iqiyi.com/m/1004287/1405160399099000/review
http://t.iqiyi.com/m/1004287/1405160345232325/review
http://t.iqiyi.com/m/1004287/14051603124196/review
http://t.iqiyi.com/m/1002964/1405160329446713/review
http://t.iqiyi.com/m/1002964/140516034299456/review
http://t.iqiyi.com/m/1002964/140516036223697/review
http://t.iqiyi.com/m/1002964/140516037450909/review
http://t.iqiyi.com/m/1002964/1405160314647500/review
http://t.iqiyi.com/m/1005932/140516036764544/review
http://t.iqiyi.com/m/1005932/1405160319520928/review
http://t.iqiyi.com/m/1005932/1405160320623512/review
http://t.iqiyi.com/m/1008256/1405160336923040/review
http://t.iqiyi.com/m/1008256/140516036462280/review
http://t.iqiyi.com/m/1008256/140516036126210/review
http://t.iqiyi.com/m/1008256/1405160341438319/review
http://t.iqiyi.com/m/1008256/14051603119709940/review
http://t.iqiyi.com/m/1008256/1405160318227370/review
http://t.iqiyi.com/m/1064357/1405160383591200/review
http://t.iqiyi.com/m/1064357/1405160331487345/review
http://t.iqiyi.com/m/1008256/140516032685525/review
http://t.iqiyi.com/m/1008832/140516033842900/review
http://t.iqiyi.com/m/1008832/1405160318808530/review
http://t.iqiyi.com/m/1008832/1405160314871779/review
http://t.iqiyi.com/m/1013819/1405160338881350/review
http://t.iqiyi.com/m/1013819/1405160322496418/review
http://t.iqiyi.com/m/1013819/140516035667120/review
http://t.iqiyi.com/m/1007843/14051603125647/review
http://t.iqiyi.com/m/1001708/140516031266160/review
http://t.iqiyi.com/m/1001708/14051603322944/review
http://t.iqiyi.com/m/1001708/140516038559408/review
http://t.iqiyi.com/m/1000223/1405160384612/review
http://t.iqiyi.com/m/1000223/14051603962325/review
http://t.iqiyi.com/m/1000223/140516031094016/review
http://t.iqiyi.com/m/1000223/140516032839346/review
http://t.iqiyi.com/m/1000223/1405160315670200/review
http://t.iqiyi.com/m/1000223/14051603387345/review
http://t.iqiyi.com/m/1000223/1405160330959005/review
http://t.iqiyi.com/m/1000886/1405160361387200/review
http://t.iqiyi.com/m/1013762/140516032325312/review
http://t.iqiyi.com/m/1013762/1405160319858176/review
http://t.iqiyi.com/m/1045259/1405160326134680/review
http://t.iqiyi.com/m/1045259/140516032475150/review
http://t.iqiyi.com/m/1045259/140516035323889/review
http://t.iqiyi.com/m/1045259/1405160354738810/review
http://t.iqiyi.com/m/1045259/1405160314603520/review
http://t.iqiyi.com/m/1045259/14051603491472/review
http://t.iqiyi.com/m/1000220/1405160322050/review
http://t.iqiyi.com/m/1015918/1405160313645579/review
http://t.iqiyi.com/m/1015918/140516037451906/review
http://t.iqiyi.com/m/1015918/140516031408407/review
http://t.iqiyi.com/m/1015918/14051603254114/review
http://t.iqiyi.com/m/1015918/140516039193554/review
http://t.iqiyi.com/m/1015918/1405160339211959/review
http://t.iqiyi.com/m/1015918/140516032132316/review
http://t.iqiyi.com/m/1010847/1405160315478320/review
http://t.iqiyi.com/m/1010847/1405160363413300/review
http://t.iqiyi.com/m/1010847/14051603492591/review
http://t.iqiyi.com/m/1010847/140516034935975/review
http://t.iqiyi.com/m/1148465/1405160313361040/review
http://t.iqiyi.com/m/1000393/140516032680587/review
http://t.iqiyi.com/m/1007873/140516035894328/review
http://t.iqiyi.com/m/1007873/14051603195453/review
http://t.iqiyi.com/m/1007873/140516037085400/review
http://t.iqiyi.com/m/1007873/1405160336199134/review
http://t.iqiyi.com/m/1007354/14051603233276579/review
http://t.iqiyi.com/m/1026327/140516036059144/review
http://t.iqiyi.com/m/1026327/1405160335270688/review
http://t.iqiyi.com/m/1064357/1405160343161026/review
http://t.iqiyi.com/m/1005742/140516033643136/review
http://t.iqiyi.com/m/1005742/140516033886254/review
http://t.iqiyi.com/m/1005742/1405160310356336/review
http://t.iqiyi.com/m/1002428/140516032605590/review
http://t.iqiyi.com/m/1002428/1405160315692768/review
http://t.iqiyi.com/m/1002428/1405160343216264/review
http://t.iqiyi.com/m/1002428/14051603833460/review
http://t.iqiyi.com/m/1002428/1405160316170720/review
public static void main(String[] args) {
EggTimer eggTimer = new EggTimer(2);
eggTimer.start();
}
}


  EggTimer 实例拥有一个 Timer 实例,用于提供必要的计划。用 start() 方法启动煮蛋计时器后,它就计划了一个 TimerTask,在指定的分钟数之后执行。时间到了,Timer 就在后台调用 TimerTask 的 start() 方法,这会使它发出声音。在取消计时器后这个应用程序就会中止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值