java主線程捕獲子線程中的異常


本文主要參考:《think in java》

好,下面上貨。

正常情況下,如果不做特殊的處理,在主線程中是不能夠捕獲到子線程中的異常的。
例如下面的情況。

   
   
  
  
          
  1. package com.xueyou.demo.theadexceptiondemo;
  2. public class ThreadExceptionRunner implements Runnable{
  3. @Override
  4. public void run() {
  5. throw new RuntimeException( "error !!!!");
  6. }
  7. }
使用線程執行上面的任務

   
   
  
  
          
  1. package com.xueyou.demo.theadexceptiondemo;
  2. import com.sun.glass.ui.TouchInputSupport;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.ThreadFactory;
  6. public class ThreadExceptionDemo {
  7. public static void main(String[] args) {
  8. try {
  9. Thread thread = new Thread( new ThreadExceptionRunner());
  10. thread.start();
  11. } catch (Exception e) {
  12. System.out.println( "========");
  13. e.printStackTrace();
  14. } finally {
  15. }
  16. System.out.println( 123);
  17. }
  18. }
執行結果如下:


如果想要在主線程中捕獲子線程的異常,我們需要使用ExecutorService,同時做一些修改。
如下:


   
   
  
  
          
  1. package com.xueyou.demo.theadexceptiondemo;
  2. import com.sun.glass.ui.TouchInputSupport;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.ThreadFactory;
  6. public class ThreadExceptionDemo {
  7. public static void main(String[] args) {
  8. try {
  9. Thread thread = new Thread( new ThreadExceptionRunner());
  10. thread.start();
  11. } catch (Exception e) {
  12. System.out.println( "========");
  13. e.printStackTrace();
  14. } finally {
  15. }
  16. System.out.println( 123);
  17. ExecutorService exec = Executors.newCachedThreadPool( new HandleThreadFactory());
  18. exec.execute( new ThreadExceptionRunner());
  19. exec.shutdown();
  20. }
  21. }
  22. class MyUncaughtExceptionHandle implements Thread.UncaughtExceptionHandler {
  23. @Override
  24. public void uncaughtException(Thread t, Throwable e) {
  25. System.out.println( "caught " + e);
  26. }
  27. }
  28. class HandleThreadFactory implements ThreadFactory {
  29. @Override
  30. public Thread newThread(Runnable r) {
  31. System.out.println( "create thread t");
  32. Thread t = new Thread(r);
  33. System.out.println( "set uncaughtException for t");
  34. t.setUncaughtExceptionHandler( new MyUncaughtExceptionHandle());
  35. return t;
  36. }
  37. }
這樣就能夠捕獲到異常了,運行結果如下:



上面的方式是設置每一個線程執行時候的異常處理。如果每一個線程的異常處理相同,我們可以用如下的方式進行處理,使用Thread的靜態方法。
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandle());

整體代碼如下:


   
   
  
  
          
  1. package com.xueyou.demo.theadexceptiondemo;
  2. import com.sun.glass.ui.TouchInputSupport;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.ThreadFactory;
  6. /**
  7. * Created by wuxueyou on 2018/ 6/ 24.
  8. */
  9. public class ThreadExceptionDemo {
  10. public static void main(String[] args) {
  11. try {
  12. Thread thread = new Thread(new ThreadExceptionRunner());
  13. thread.start();
  14. } catch (Exception e) {
  15. System.out.println("========");
  16. e.printStackTrace();
  17. } finally {
  18. }
  19. System.out.println(123);
  20. Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandle());
  21. // ExecutorService exec = Executors.newCachedThreadPool(new HandleThreadFactory());
  22. ExecutorService exec = Executors.newCachedThreadPool();
  23. exec.execute(new ThreadExceptionRunner());
  24. exec.shutdown();
  25. }
  26. }
運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM