JAVA同時、スレッド異常キャプチャ

11689 ワード

スレッドの特性のため、スレッドを起動すると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