码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA并发,线程异常捕获

时间:2015-04-22 23:32:01      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

由于线程的特性,当我们启动了线程是没有办法用try catch捕获异常的,如下例:

 1 package com.xt.thinks21_2;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 
 6 /**
 7  * 线程异常捕获测试
 8  * 
 9  * @author xue
10  *
11  */
12 public class ThreadUncaughtExceptionTest implements Runnable {
13 
14     @Override
15     public void run() {
16         // TODO Auto-generated method stub
17         throw new NullPointerException();
18     }
19 
20     public static void main(String[] args) {
21         try {
22             ExecutorService es = Executors.newCachedThreadPool();
23             es.execute(new ThreadUncaughtExceptionTest());
24             es.shutdown();
25             // or this
26             // Thread t = new Thread(new ThreadUncaughtExceptionTest());
27             // t.start();
28         } catch (Exception e) {
29             // TODO: handle exception
30         }
31 
32     }
33 }

打印异常信息为:

Exception in thread "pool-1-thread-1" java.lang.NullPointerException
at com.xt.thinks21_2.ThreadUncaughtExceptionTest.run(ThreadUncaughtExceptionTest.java:17)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

此时,我们需要为线程定制工厂类来生产线程,为线程指定UncaughtExceptionHandler

 1 package com.xt.thinks21_2;
 2 
 3 import java.lang.Thread.UncaughtExceptionHandler;
 4 import java.util.concurrent.ExecutorService;
 5 import java.util.concurrent.Executors;
 6 import java.util.concurrent.ThreadFactory;
 7 
 8 /**
 9  * 未捕获异常执行程序
10  * 
11  * @author Administrator
12  *
13  */
14 class ThreadUncaughtExceptionHandler implements UncaughtExceptionHandler {
15 
16     @Override
17     public void uncaughtException(Thread t, Throwable e) {
18         // TODO Auto-generated method stub
19         System.out.println("caught:" + e);
20     }
21 
22 }
23 
24 /**
25  * 线程工厂
26  * 
27  * @author Administrator
28  *
29  */
30 class HandlerThreadExceptionFactory implements ThreadFactory {
31 
32     @Override
33     public Thread newThread(Runnable r) {
34         // TODO Auto-generated method stub
35         Thread t = new Thread(r);
36         t.setUncaughtExceptionHandler(new ThreadUncaughtExceptionHandler());
37         return t;
38     }
39 
40 }
41 
42 /**
43  * 线程异常捕获测试
44  * 
45  * @author xue
46  *
47  */
48 public class ThreadUncaughtExceptionTest implements Runnable {
49 
50     @Override
51     public void run() {
52         // TODO Auto-generated method stub
53         throw new NullPointerException();
54     }
55 
56     public static void main(String[] args) {
57         ExecutorService es = Executors
58                 .newCachedThreadPool(new HandlerThreadExceptionFactory());
59         es.execute(new ThreadUncaughtExceptionTest());
60         es.shutdown();
61     }
62 }

此时,我们就可以看到异常信息被捕获到了:

caught:java.lang.NullPointerException

JAVA并发,线程异常捕获

标签:

原文地址:http://www.cnblogs.com/wubingshenyin/p/4448980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!