Timer和ScheduledExecutorService区别

Timer特点:

 
1、一个Timer只占用一个线程
timer有多个timerTask的情况,如果一个timerTask有执行时间过长,其它的timerTask就会被耽误
2、如果TimerTask抛出未检查的异常,Timer会由于该异常导致中断。后续的TimerTask任务也不会执行
示例:
 1 import java.util.Date;
 2 import java.util.Timer;
 3 import java.util.TimerTask;
 4  
 5 public class TimerTest extends TimerTask {
 6  
 7         private String jobName = "" ;
 8  
 9         public TimerTest(String jobName) {
10                super();
11                this.jobName = jobName;
12        }
13  
14         int count = 0;
15  
16         @Override
17         public void run() {
18               System. out.println(jobName + " " + new Date() + " beep " + (++count));
19                try {
20                       if (jobName .equals("job1" )) {
21                            Thread. sleep(1000);
22                             throw new RuntimeException();
23                      }
24  
25               } catch (InterruptedException e) {
26                       // TODO Auto-generated catch block
27                      e.printStackTrace();
28               }
29        }
30  
31         public static void main(String[] args) {
32               Timer timer = new Timer();
33                long period = 2000;
34                // 从现在开始 1 秒钟之后,每隔 2秒钟执行一次 job1
35               timer.schedule( new TimerTest("job1" ), 0, period);
36                // 每隔 2 秒钟执行一次 job2
37               timer.schedule( new TimerTest("job2" ), 0, period);
38        }
39 }
40 执行结果
41 job1 Tue Apr 08 23:37:17 CST 2014 beep 1
42 Exception in thread "Timer-0" java.lang.RuntimeException
43        at TimerTest.run( TimerTest.java:20)
44        at java.util.TimerThread.mainLoop( Timer.java:512)
45        at java.util.TimerThread.run( Timer.java:462)

 

ScheduledExecutorService(实现类ScheduledThreadPoolExecutor)特点:
1、ScheduledExecutorService是多线程
只要线程池不设置为1,每个线程任务都能按照各自的频率执行。
2、如果线程任务抛出未检查的异常,ScheduledExecutorService只是中断该线程任务,其它任务正常执行
示例:
 1 import static java.util.concurrent.TimeUnit.SECONDS;
 2 import java.util.Date;
 3 import java.util.TimerTask;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.ScheduledExecutorService;
 6 import java.util.concurrent.ScheduledFuture;
 7  
 8 public class TestScheduledThread extends TimerTask {
 9  
10         private String jobName = "" ;
11  
12         public TestScheduledThread(String jobName) {
13                super();
14                this.jobName = jobName;
15        }
16  
17         int count = 0;
18  
19         @Override
20         public void run() {
21               System. out.println(jobName + " " + new Date() + " beep " + (++count));
22                try {
23                       if (jobName .equals("job1" )) {
24                            Thread. sleep(1000);
25                             throw new RuntimeException();
26                      }
27  
28               } catch (InterruptedException e) {
29                       // TODO Auto-generated catch block
30                      e.printStackTrace();
31               }
32        }
33  
34         public static void main(String[] args) {
35                final ScheduledExecutorService scheduler = Executors
36                            . newScheduledThreadPool(2);
37                //每隔2秒运行一次
38                final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(
39                             new TimerTest("job1" ), 0, 2, SECONDS);
40                //每隔2秒运行一次
41                final ScheduledFuture beeperHandle2 = scheduler.scheduleAtFixedRate(
42                             new TimerTest("job2" ), 0, 2, SECONDS);
43        }
44 }

 

 
执行结果:
job2 Tue Apr 08 23:42:58 CST 2014 beep 1
job1 Tue Apr 08 23:42:58 CST 2014 beep 1
job2 Tue Apr 08 23:43:00 CST 2014 beep 2
job2 Tue Apr 08 23:43:02 CST 2014 beep 3
job2 Tue Apr 08 23:43:04 CST 2014 beep 4
job2 Tue Apr 08 23:43:06 CST 2014 beep 5
job2 Tue Apr 08 23:43:08 CST 2014 beep 6

posted on 2014-04-10 22:01  飞凡_gz  阅读(670)  评论(0编辑  收藏  举报

导航