Java中Timer的使用
来源:互联网 发布:jenkins 配置mac节点 编辑:程序博客网 时间:2024/06/05 10:29
在应用开发中,经常需要一些周期性的操作,比如每5分钟检查一下新邮件等。对于这样的操作最方便、高效的实现方式就是使用java.util.Timer工具类。比如下面的代码每5分钟检查一遍是否有新邮件:
<!--[if !supportLists]-->1. <!--[endif]-->private java.util.Timer timer;
<!--[if !supportLists]-->2. <!--[endif]-->timer = new Timer(true);
<!--[if !supportLists]-->3. <!--[endif]-->timer.schedule(new java.util.TimerTask() {
<!--[if !supportLists]-->4. <!--[endif]-->public void run() {
<!--[if !supportLists]-->5. <!--[endif]-->//server.checkNewMail(); 检查新邮件
<!--[if !supportLists]-->6. <!--[endif]-->}
<!--[if !supportLists]-->7. <!--[endif]-->}, 0, 5*60*1000);
使用这几行代码之后,Timer本身会每隔5分钟调用一遍server.checkNewMail()方法,不需要自己启动线程。Timer本身也是多线程同步的,多个线程可以共用一个Timer,不需要外部的同步代码。
在《The Java Tutorial》中有更完整的例子:
<!--[if !supportLists]-->1. <!--[endif]-->public class AnnoyingBeep {
<!--[if !supportLists]-->2. <!--[endif]-->Toolkit toolkit;
<!--[if !supportLists]-->3. <!--[endif]-->Timer timer;
<!--[if !supportLists]-->4. <!--[endif]-->
<!--[if !supportLists]-->5. <!--[endif]-->public AnnoyingBeep() {
<!--[if !supportLists]-->6. <!--[endif]-->toolkit = Toolkit.getDefaultToolkit();
<!--[if !supportLists]-->7. <!--[endif]-->timer = new Timer();
<!--[if !supportLists]-->8. <!--[endif]-->timer.schedule(new RemindTask(),
<!--[if !supportLists]-->9. <!--[endif]-->0, //initial delay
<!--[if !supportLists]-->10. <!--[endif]-->1*1000); //subsequent rate
<!--[if !supportLists]-->11. <!--[endif]-->}
<!--[if !supportLists]-->12. <!--[endif]-->
<!--[if !supportLists]-->13. <!--[endif]-->class RemindTask extends TimerTask {
<!--[if !supportLists]-->14. <!--[endif]-->int numWarningBeeps = 3;
<!--[if !supportLists]-->15. <!--[endif]-->
<!--[if !supportLists]-->16. <!--[endif]-->public void run() {
<!--[if !supportLists]-->17. <!--[endif]-->if (numWarningBeeps > 0) {
<!--[if !supportLists]-->18. <!--[endif]-->toolkit.beep();
<!--[if !supportLists]-->19. <!--[endif]-->System.out.println("Beep!");
<!--[if !supportLists]-->20. <!--[endif]-->numWarningBeeps--;
<!--[if !supportLists]-->21. <!--[endif]-->} else {
<!--[if !supportLists]-->22. <!--[endif]-->toolkit.beep();
<!--[if !supportLists]-->23. <!--[endif]-->System.out.println("Time's up!");
<!--[if !supportLists]-->24. <!--[endif]-->//timer.cancel(); //Not necessary because we call System.exit
<!--[if !supportLists]-->25. <!--[endif]-->System.exit(0); //Stops the AWT thread (and everything else)
<!--[if !supportLists]-->26. <!--[endif]-->}
<!--[if !supportLists]-->27. <!--[endif]-->}
<!--[if !supportLists]-->28. <!--[endif]-->}
<!--[if !supportLists]-->29. <!--[endif]-->...
<!--[if !supportLists]-->30. <!--[endif]-->}
这段程序,每隔3秒响铃一声,并打印出一行消息。循环3次。程序输出如下:
Task scheduled.
Beep!
Beep! //one second after the first beep
Beep! //one second after the second beep
Time's up! //one second after the third beep
Timer类也可以方便地用来作为延迟执行,比如下面的代码延迟指定的时间(以秒为单位)执行某操作。类似电视的延迟关机功能。
<!--[if !supportLists]-->1. <!--[endif]-->...
<!--[if !supportLists]-->2. <!--[endif]-->public class ReminderBeep {
<!--[if !supportLists]-->3. <!--[endif]-->...
<!--[if !supportLists]-->4. <!--[endif]-->public ReminderBeep(int seconds) {
<!--[if !supportLists]-->5. <!--[endif]-->toolkit = Toolkit.getDefaultToolkit();
<!--[if !supportLists]-->6. <!--[endif]-->timer = new Timer();
<!--[if !supportLists]-->7. <!--[endif]-->timer.schedule(new RemindTask(), seconds*1000);
<!--[if !supportLists]-->8. <!--[endif]-->}
<!--[if !supportLists]-->9. <!--[endif]-->
<!--[if !supportLists]-->10. <!--[endif]-->class RemindTask extends TimerTask {
<!--[if !supportLists]-->11. <!--[endif]-->public void run() {
<!--[if !supportLists]-->12. <!--[endif]-->System.out.println("Time's up!");
<!--[if !supportLists]-->13. <!--[endif]-->toolkit.beep();
<!--[if !supportLists]-->14. <!--[endif]-->//timer.cancel(); //Not necessary because we call System.exit
<!--[if !supportLists]-->15. <!--[endif]-->System.exit(0); //Stops the AWT thread (and everything else)
<!--[if !supportLists]-->16. <!--[endif]-->}
<!--[if !supportLists]-->17. <!--[endif]-->}
<!--[if !supportLists]-->18. <!--[endif]-->...
- java中Timer的使用
- Java中Timer的使用
- Java中Timer的使用
- Java中Timer的使用
- Java中Timer的使用
- java中timer的使用
- java中Timer的使用
- Java中Timer,TimerTask的使用
- Java中Timer,TimerTask的使用
- java中Timer使用的简要说明
- java中Timer使用的简要说明
- java中timer类的使用
- java中定期执行--Timer的使用
- Java中Timer和TimerTask的使用
- JAVA中定时器Timer的使用
- java中Timer定时器的使用
- Java中Timer和TimerTask的使用
- java中Timer定时器的使用
- DataGridView回车换行
- java网络编程-Ajax+servlet实例
- AJAX+servlet实例入门
- AJAX在servlet中返回中文乱码问题的解决
- 如何用java和access连接
- Java中Timer的使用
- java中Timer使用的简要说明
- 使用Java中的Timer和TimerTask
- java中Date的用法
- innodb在线备份的新方法
- 初探SQL Server 2008商业智能
- Servlet中如何捕获Session事件
- C语言:关于计算字符串中空格数的问题
- Derby 教程