java多執行緒:執行緒體往外丟擲異常的處理機制實踐

Love Lenka發表於2017-03-16

1當執行緒的執行緒體內部無捕獲異常,將異常丟擲執行緒體外,不同情況下,程式處理機制

測試類

 1 package com.ehking.bankchannel.domesticremit.facade.impl;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.concurrent.Executor;
 6 import java.util.concurrent.Executors;
 7 
 8 
 9 public class ThreadTest {
10 
11     private static Executor executor=Executors.newCachedThreadPool();
12     
13     
14     public static void main(String[] args) {
15         test01();
16         //test02();
17     }
18     
19     /**
20      * 執行緒池,主執行緒往執行緒池提交任務(子執行緒)
21      * 如果任務中有往外丟擲異常,主執行緒並不會捕獲異常。
22      * 是執行緒池捕獲了任務的異常
23      */
24     public static void test02(){
25         
26         ThreadTask aTask=new ThreadTask(1);
27         ThreadTask aTask2=new ThreadTask(2);
28         ThreadTask aTask3=new ThreadTask(6);
29         List<ThreadTask> aList=new ArrayList<ThreadTask>();
30         aList.add(aTask);
31         aList.add(aTask2);
32         aList.add(aTask3);
33         try {
34             for(ThreadTask a:aList){
35                 executor.execute(a);
36             }
37             System.out.println("ThreadTest.test02(沒有執行)");
38         } catch (Exception e) {
39             System.out.println("ThreadTest.test02()"+e);
40         }
41         
42         
43     }
44     /**
45      * 如果用匿名執行緒,匿名執行緒中有丟擲異常,無捕獲的話,主執行緒是會捕獲這個異常
46      */
47     public static  void test01(){
48         System.out.println("ThreadTest.test01()==>主執行緒執行開始");
49         final List<String> list=new ArrayList<String>();
50         try {
51             list.add("sxf");
52             list.add("chn");
53             
54             Thread thread=new Thread(new Runnable() {
55                 
56                 @Override
57                 public void run() {
58                     System.out
59                             .println("ThreadTest.test01().new Runnable() {...}.run()子執行緒加愛");
60                     list.add("love");
61                     System.out
62                             .println("ThreadTest.test01().new Runnable() {...}.run()子執行緒丟擲異常");
63                     int a=3/0;
64                     System.out
65                             .println("ThreadTest.test01().new Runnable() {...}.run()z縣城結束");
66                 }
67             });
68             thread.start();
69             
70             
71         } catch (Exception e) {
72             System.out.println("ThreadTest.test01()子執行緒丟擲異常"+e);
73         }
74         
75         try {
76             Thread.sleep(3000L);
77         } catch (InterruptedException e) {
78             // TODO Auto-generated catch block
79             e.printStackTrace();
80         }
81         for(String aString:list){
82             System.out.println("ThreadTest.test01()列印==>"+aString);
83         }
84         System.out.println("ThreadTest.test01()==>主執行緒執行結束");
85     }
86 }
View Code

 執行緒類

 1 package com.ehking.bankchannel.domesticremit.facade.impl;
 2 
 3 public class ThreadTask implements Runnable{
 4 
 5     private int a;
 6     
 7     public ThreadTask(int a){
 8         this.a=a;
 9     }
10     
11     @Override
12     public void run() {
13         if(a%2==0){
14             System.out.println("ThreadTask.run()【"+a+"】執行正常.......");
15         }else{
16             System.out.println("ThreadTask.run()【"+a+"】丟擲異常");
17             int s=2/0;
18         }
19         
20     }
21 
22     
23 }
View Code

 

相關文章