溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

詳解Java中多線程異常捕獲Runnable的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-09-19 00:36:09 來(lái)源:腳本之家 閱讀:319 作者:_QING_FENG 欄目:編程語(yǔ)言

詳解Java中多線程異常捕獲Runnable的實(shí)現(xiàn)

1、背景:

        Java 多線程異常不向主線程拋,自己處理,外部捕獲不了異常。所以要實(shí)現(xiàn)主線程對(duì)子線程異常的捕獲。
2、工具:

        實(shí)現(xiàn)Runnable接口的LayerInitTask類(lèi),ThreadException類(lèi),線程安全的Vector
3、思路:

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

4、代碼:     

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("根據(jù)返回值捕獲:exception"); 
        throw new RuntimeException( "入庫(kù)失??!"); 
      } 
       
    } catch (Exception e) { 
      System.out.println("根據(jù)拋出異常捕獲:exception"); 
      throw new RuntimeException( "入庫(kù)失??!"); 
    } 
    System.out.println("-----入庫(kù)成功,發(fā)成功完成工作郵件--------"); 
  } 
 
} 
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 + ":數(shù)據(jù)格式有誤."); 
      } 
      System.out.println(this.threadNum + ":刷表成功"); 
    } catch (Exception e) { 
      this.errRet.add("線程:" + this.threadNum + "運(yùn)行異常!"); 
      throw new RuntimeException( this.threadNum + ":刷表失敗"); 
    } 
  } 
 
} 

5、結(jié)果:

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: 入庫(kù)失?。?
  at step5.exception.ThreadException.main(ThreadException.java:27) 
2:刷表成功 
1:刷表成功 
5:刷表成功 
0:刷表成功 
4:刷表成功 
根據(jù)返回值捕獲:exception 
根據(jù)拋出異常捕獲:exception 

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI