從頭認識java-18.2 基本的線程機制(8)多線程的異常捕捉

來源:互聯網
上載者:User

從頭認識java-18.2 基本的線程機制(8)多線程的異常捕捉

這一章節我們來討論一下多線程的異常捕捉。

1.普通情況的異常

package com.ray.ch17;public class Test {public static void main(String[] args) {try {new ThreadA().run();} catch (Exception e) {System.out.println("捕捉到異常");}}}class ThreadA implements Runnable {@Overridepublic void run() {throw new RuntimeException();}}

輸出:

捕捉到異常

2.多線程的異常

package com.ray.ch17;public class Test {public static void main(String[] args) {try {new Thread(new ThreadA()).start();} catch (Exception e) {System.out.println("捕捉到異常");}}}class ThreadA implements Runnable {@Overridepublic void run() {throw new RuntimeException();}}

輸出:

Exception in thread "Thread-0" java.lang.RuntimeException
at com.ray.ch17.ThreadA.run(Test.java:18)
at java.lang.Thread.run(Thread.java:662)

3.問題:

在普通的情況下,我們可以順利的捕捉到異常,但是如果在多線程的情況下,捕捉異常的方式其實不是上面的那種

4.解決方案

實現Thread.UncaughtExceptionHandler介面

(1)使用普通的方式來實現

package com.ray.ch17;public class Test {public static void main(String[] args) {try {Thread thread = new Thread(new ThreadA());thread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());thread.start();} catch (Exception e) {System.out.println("捕捉到異常");}}}class ThreadA implements Runnable {@Overridepublic void run() {throw new RuntimeException();}}class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {@Overridepublic void uncaughtException(Thread t, Throwable e) {System.out.println("捕捉到:" + t.getName() + "的異常");}}
輸出:

捕捉到:Thread-0的異常

(2)使用線程池的方式來實現

package com.ray.ch17;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ThreadFactory;public class Test {public static void main(String[] args) {ExecutorService executorService = Executors.newCachedThreadPool(new MyThreadFactory());executorService.execute(new ThreadA());executorService.shutdown();}}class ThreadA implements Runnable {@Overridepublic void run() {throw new RuntimeException();}}class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {@Overridepublic void uncaughtException(Thread t, Throwable e) {System.out.println("捕捉到:" + t.getName() + "的異常");}}class MyThreadFactory implements ThreadFactory {@Overridepublic Thread newThread(Runnable r) {Thread thread = new Thread(r);thread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());return thread;}}

輸出:

捕捉到:Thread-0的異常

總結:這一章節比較詳細的討論了多線程的異常捕捉。

這一章節就到這裡,謝謝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.