Timer 和 ScheduleExecutorService 区别 (转)

原文: http://sunnylocus.iteye.com/blog/530969

-----------------------------------------------

java.util.Timer计时器有管理任务延迟执行("如1000ms后 执行任务")以及周期性执行("如每500ms执行一次该任务")。但是,Timer存在一些缺陷,因此你应该考虑使用 ScheduledThreadPoolExecutor作为代替品,Timer对调度的支持是基于绝对时间,而不是相对时间的,由此任务对系统时钟的改 变是敏感的;ScheduledThreadExecutor只支持相对时间。

    Timer的另一个问题在于,如果TimerTask抛出未检查的异常,Timer将会产生无法预料的行为。Timer线程并不捕获异常,所以 TimerTask抛出的未检查的异常会终止timer线程。这种情况下,Timer也不会再重新恢复线程的执行了;它错误的认为整个Timer都被取消 了。此时,已经被安排但尚未执行的TimerTask永远不会再执行了,新的任务也不能被调度了。

 测试Timer的例子

 

View Code
package threadDemo.TimerAndScheduledExecutorDemo;

import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {
private Timer timer = new Timer();

public void launchTimer() {
timer.schedule(new TimerTask(){
@Override
public void run() {
throw new RuntimeException();
}
}, 1000*3, 500);
}

public void addOneTask() {
timer.schedule(new TimerTask(){
@Override
public void run() {
System.out.println("hello world");
}
}, 1000*1, 1000*5);
}

public static void main(String[] args) throws InterruptedException {
TimerTest test = new TimerTest();
test.launchTimer();
Thread.sleep(5000);
test.addOneTask();
}
}

 

运行该程序,Timer会抛出一个RumtimeExceptionjava.lang.IllegalStateException:Timer already cancelled.

 

常言道,真是祸不单行,Timer还将它的问题传染给下一个倒霉的调用者,这个调用者原本试图提交一个TimerTask的,你可能希望程序会一直运行下去,然而实际情况如程序所示5秒钟后就中止了,还伴随着一个异常,异常的消息是"Timer already cancelled"。ScheduledThreadPoolExector妥善地处理了这个异常的任务,所以说在java5.0或更高的JDK中,几乎没有理由再使用Timer了。

 

View Code
Exception in thread "Timer-0" java.lang.RuntimeException
at threadDemo.TimerAndScheduledExecutorDemo.TimerTest$1.run(TimerTest.java:14)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Exception in thread "main" java.lang.IllegalStateException: Timer already cancelled.
at java.util.Timer.sched(Timer.java:354)
at java.util.Timer.schedule(Timer.java:222)
at threadDemo.TimerAndScheduledExecutorDemo.TimerTest.addOneTask(TimerTest.java:21)
at threadDemo.TimerAndScheduledExecutorDemo.TimerTest.main(TimerTest.java:35)


用ScheduledThreadPoolExector改进后的例子

 

View Code
package threadDemo.TimerAndScheduledExecutorDemo;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorTest {
public ScheduledExecutorService scheduledExec = Executors.newScheduledThreadPool(1);
int i = 0;
int j = 0;
public void launchTimer() {
Runnable task = new Runnable(){
@Override
public void run() {
System.out.println("launch: " + ++j);
throw new RuntimeException();
}

};
scheduledExec.scheduleWithFixedDelay(task, 1000*5, 1000*10, TimeUnit.MILLISECONDS);
}

public void addOneTask() {
Runnable task = new Runnable(){
@Override
public void run() {
System.out.println("hello world: " + ++i);
}
};
scheduledExec.scheduleWithFixedDelay(task, 1000*1, 1000, TimeUnit.MILLISECONDS);
}

public static void main(String[] args) throws InterruptedException {
ScheduledExecutorTest test = new ScheduledExecutorTest();
test.launchTimer();
Thread.sleep(5000);
test.addOneTask();
}
}

结果:

 

View Code
launch: 1
hello world: 1
hello world: 2
hello world: 3
hello world: 4
hello world: 5
hello world: 6
...






posted @ 2011-12-20 17:48  名字只是个称号而已  阅读(666)  评论(0编辑  收藏  举报