一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Java中多線程異常捕獲Runnable的實現

詳解Java中多線程異常捕獲Runnable的實現

2021-01-19 10:32_QING_FENG Java教程

這篇文章主要介紹了詳解Java中多線程異常捕獲Runnable的實現的相關資料,希望通過本文能幫助到大家,讓大家理解掌握這樣的知識,需要的朋友可以參考下

詳解Java多線程異常捕獲Runnable的實現

1、背景:

        Java 多線程異常不向主線程拋,自己處理,外部捕獲不了異常。所以要實現主線程對子線程異常的捕獲。
2、工具:

        實現Runnable接口的LayerInitTask類,ThreadException類,線程安全的Vector
3、思路:

       向LayerInitTask中傳入Vector,記錄異常情況,外部遍歷,判斷,拋出異常。

4、代碼:     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package step5.exception;
 
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
import com.autonavi.pds.core.incre.impl.LayerInitTask;
 
public class ThreadException {
 
  public static void main(String[] args) {
    try {
      Vector<String> errRet = new Vector();
      ExecutorService pool = Executors.newFixedThreadPool(6);
      for (int i = 0; i < 6; ++i) {
        pool.execute(new LayerInitTask(i, errRet));
      }
      pool.shutdown();
      pool.awaitTermination(1, TimeUnit.DAYS);
       
      if (errRet.size() > 0) {
        System.out.println("根據返回值捕獲:exception");
        throw new RuntimeException( "入庫失敗!");
      }
       
    } catch (Exception e) {
      System.out.println("根據拋出異常捕獲:exception");
      throw new RuntimeException( "入庫失敗!");
    }
    System.out.println("-----入庫成功,發成功完成工作郵件--------");
  }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package step5.exception;
 
import java.util.Vector;
 
public class LayerInitTask implements Runnable {
  private int threadNum;
  private Vector<String> errRet;
 
  public LayerInitTask(int num, Vector<String> errRet) {
    this.threadNum = num;
    this.errRet = errRet;
  }
 
  @Override
  public void run() {
    try {
      if (this.threadNum == 3) {
        throw new RuntimeException( this.threadNum + ":數據格式有誤.");
      }
      System.out.println(this.threadNum + ":刷表成功");
    } catch (Exception e) {
      this.errRet.add("線程:" + this.threadNum + "運行異常!");
      throw new RuntimeException( this.threadNum + ":刷表失敗");
    }
  }
 
}

5、結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Exception in thread "pool-1-thread-4" java.lang.RuntimeException: 3:刷表失敗
  at step5.exception.LayerInitTask.run(LayerInitTask.java:23)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
Exception in thread "main" java.lang.RuntimeException: 入庫失敗!
  at step5.exception.ThreadException.main(ThreadException.java:27)
2:刷表成功
1:刷表成功
5:刷表成功
0:刷表成功
4:刷表成功
根據返回值捕獲:exception
根據拋出異常捕獲:exception

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/q_l_s/article/details/70159749

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本在线亚州精品视频在线 | 好大好热 | 亚洲视频在线一区二区三区 | 国产欧美视频在线观看 | 亚洲欧美精品天堂久久综合一区 | 日本在线观看www鲁啊鲁视频 | 欧美日韩久久中文字幕 | 国产综合亚洲欧美日韩一区二区 | mm131亚洲| 国产中文在线视频 | 四虎影视在线观看永久地址 | 男女刺激高清视频在线观看 | 国产成人精品视频午夜 | 国产成人啪精品午夜在线播放 | 免费一级特黄特色大片 | 91麻豆精东果冻天美传媒老狼 | 欧美国产精品 | 2021国产精品成人免费视频 | 亚洲欧美综合区自拍另类 | 思思99热久久精品在2019线 | 国产高清经典露脸3p | 91国产高清 | 欧洲美女bbbxxxxxx| 欧美性xxx狂流白浆 欧美性f | aaaa大片| 男人天堂官方网站 | 国产精品久久久久久久午夜片 | 五月最新商场女厕所高跟嘘嘘 | 俄罗斯伦理片 | 欧美人与禽交片在线播放 | 色综色天天综合网 | 网站久久 | 大象视频污 | 91制片厂制作传媒网站 | 成人精品免费网站 | 亚洲 日韩 国产 制服 在线 | 99精品视频免费在线观看 | 日本tube24xxxxx | 久久三级视频 | 欧美日韩精品一区二区三区视频 | 天天综合色天天综合 |