Java 定时器退出问题

项目中用到了 Timer 每隔一段时间进行一些操作,现在发现有时候莫名其妙地挂在这个计时器上的操作都不做了,用“JConsole”查看其中的线程以后,发现这个定时器线程也不在了(定时器创建的时候带了名字 Timer timer = new Timer("MyTimer"),所以直接能看到),由于日志太多,之前的日志已经找不到了,所以没有办法看是否是有异常发生以及发生在哪里。初步估计,是不是由于 TimerTask 中有异常抛出,如果定时器中没有处理的话,可能就会出问题。所以看了一下 java.util.Timer 的代码:

  1. // 在 TimerThread 中执行任务
  2. Timer.java:101:TimerThread
  3.   // TimerThread 的入口
  4.   // 这里只有一个 try/finally,如果 mainloop 中有异常抛出的话,肯定就结束运行。
  5.   Timer.java:503:TimerThread.run()
  6.     // 主循环体,其中只抓住了 InterruptedException 异常,其他的仍然往外抛。
  7.     Timer.java:518:TimerThread.mainloop()

从上面的代码可以看出,如果执行的任务中有其他任何运行时异常(RuntimeException)抛出,就必然导致定时器取消,也就是说挂在这个定时器上所有的人物都要被取消。明白了其内部处理机制,就要将其应用于实践,看如下例子:

  1. WrongTimerTask.java:
  2.  
  3. package org.supermmx.example.timer;
  4.  
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. /**
  9.  * Wrong timer task.
  10.  *
  11.  * @author SuperMMX
  12.  */
  13. public class WrongTimerTask extends TimerTask {
  14.     private int count = 0;
  15.  
  16.     public void run() {
  17.         System.out.println(count);
  18.         count ++;
  19.         if (count == 3) {
  20.             throw new NullPointerException("Test Exception");
  21.         }
  22.     }
  23.  
  24.     public static void main(String[] args) {
  25.         try {
  26.             WrongTimerTask task = new WrongTimerTask();
  27.             Timer timer = new Timer("TestTimer");
  28.             timer.scheduleAtFixedRate(task, 0, 1000);
  29.         } catch (Exception e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33. }

上述代码中,每隔一秒钟打印一个递增的数值,等于 3 的时候抛一个空指针异常,结果怎么样呢?整个程序全部就退出了,就因为其中唯一的线程“TestTimer”退出了。跟前面所说的问题一模一样,只不过项目中还有很多别的线程,所以主程序并未退出。

解决方法是什么呢?其实很简单,将 TimerTask 中整个 run() 方法 try 起来就可以了,保证它不再往外抛异常,代码如下:

  1. RightTimerTask.java:
  2.  
  3. package org.supermmx.example.timer;
  4.  
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. /**
  9.  * Wrong timer task.
  10.  *
  11.  * @author SuperMMX
  12.  */
  13. public class RightTimerTask extends TimerTask {
  14.     private int count = 0;
  15.  
  16.     public void run() {
  17.         try {
  18.             System.out.println(count);
  19.             count ++;
  20.             if (count == 3) {
  21.                 throw new NullPointerException("Test Exception");
  22.             }
  23.         } catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.     public static void main(String[] args) {
  29.         try {
  30.             RightTimerTask task = new RightTimerTask();
  31.             Timer timer = new Timer("TestTimer");
  32.             timer.scheduleAtFixedRate(task, 0, 1000);
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37. }

结果就是:异常也仍然抓住了,定时器也仍然可以继续工作,达到我们本来的目的。

结论:使用 java.util.Timer 时候,在 java.util.TimerTaskrun() 方法中实现具体操作的时候,必须要抓住所有异常,尤其是 RuntimeException,必须要保证不能往外抛异常,才能保证跟预想的运行情况一致。

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You can use BBCode tags in the text.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
_ltravi_let: