<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 如何使用`UncaughtExceptionHandler`重新啟動線程 > 原文: [https://howtodoinjava.com/java/multi-threading/how-to-restart-thread-using-uncaughtexceptionhandler/](https://howtodoinjava.com/java/multi-threading/how-to-restart-thread-using-uncaughtexceptionhandler/) 我們已經知道 Java 中有兩種異常。 受檢的異常和非受檢的異常。 必須在方法的`throws`子句中指定受檢的異常或將其捕獲在其中。 無需指定或捕獲非受檢的異常。 當在`Thread`對象的`run()`方法內引發受檢異常時,由于`run()`方法不接受`throws`子句,因此我們必須相應地對其進行處理。 但是,當在`Thread`對象的`run()`方法內引發非受檢的異常時,默認行為是在控制臺中寫入棧跟蹤(或將其記錄在錯誤日志文件中)并退出程序。 幸運的是,Java 為我們提供了一種機制來捕獲和處理`Thread`對象中拋出的非受檢的異常,從而避免程序結束。 這可以使用`UncaughtExceptionHandler`完成。 讓我們以`UncaughtExceptionHandler`用法為例。 在此示例中,我們創建了一個線程,該線程嘗試解析一些應該為整數的字符串。 我們編寫了`run()`方法,使其在執行過程中拋出“ `java.lang.NumberFormatException`”。 由于程序不會嘗試捕獲此異常,因此異常會在 JVM 級別浮動,并且線程被殺死。 這絕對是正常行為,但可能不是您期望的行為。 ## 不使用`UncaughtExceptionHandler` 在現實生活中的應用程序中,即使幾次失敗,您也想嘗試執行一次多次重要任務。 下面的示例演示了用例,首先不使用`UncaughtExceptionHandler`; 導致線程在失敗后立即死亡。 **`Task.java`** ```java class Task implements Runnable { @Override public void run() { System.out.println(Integer.parseInt("123")); System.out.println(Integer.parseInt("234")); System.out.println(Integer.parseInt("345")); System.out.println(Integer.parseInt("XYZ")); //This will cause NumberFormatException System.out.println(Integer.parseInt("456")); } } ``` **`DemoThreadExample.java`** ```java public class DemoThreadExample { public static void main(String[] args) { Task task = new Task(); Thread thread = new Thread(task); thread.start(); } } ``` 下面是運行線程時得到的輸出: ```java 123 234 345 Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: "XYZ" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at examples.algorithms.sleepingbarber.Task.run(DemoThreadExample.java:24) at java.lang.Thread.run(Unknown Source) ``` ## 使用`UncaughtExceptionHandler`之后 讓我們添加一個`UncaughtExceptionHandler`實現,以在運行時捕獲任何非受檢的異常。 **`ExceptionHandler.java`** ```java class ExceptionHandler implements UncaughtExceptionHandler { public void uncaughtException(Thread t, Throwable e) { System.out.printf("An exception has been captured\n"); System.out.printf("Thread: %s\n", t.getId()); System.out.printf("Exception: %s: %s\n", e.getClass().getName(), e.getMessage()); System.out.printf("Stack Trace: \n"); e.printStackTrace(System.out); System.out.printf("Thread status: %s\n", t.getState()); new Thread(new Task()).start(); } } ``` 現在,將此異常處理程序添加到線程中。 ```java class Task implements Runnable { @Override public void run() { Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler()); System.out.println(Integer.parseInt("123")); System.out.println(Integer.parseInt("234")); System.out.println(Integer.parseInt("345")); System.out.println(Integer.parseInt("XYZ")); //This will cause NumberFormatException System.out.println(Integer.parseInt("456")); } } ``` 現在再次運行上面的示例。 這將連續運行。 在現實生活中,如果該任務能夠完成任務,那么它將在不引發任何異常的情況下退出并完成其生命周期。 ```java 123 234 345 An exception has been captured Thread: 1394 Exception: java.lang.NumberFormatException: For input string: "XYZ" Stack Trace: java.lang.NumberFormatException: For input string: "XYZ" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at examples.algorithms.sleepingbarber.Task.run(DemoThreadExample.java:24) at java.lang.Thread.run(Unknown Source) Thread status: RUNNABLE 123 234 345 An exception has been captured Thread: 1395 Exception: java.lang.NumberFormatException: For input string: "XYZ" Stack Trace: java.lang.NumberFormatException: For input string: "XYZ" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at examples.algorithms.sleepingbarber.Task.run(DemoThreadExample.java:24) at java.lang.Thread.run(Unknown Source) Thread status: RUNNABLE 123 234 345 ``` 上面的實現可幫助您以某種方式運行線程,直到執行任務為止。 這也可以通過其他多線程概念來實現。 **請注意,`UncaughtExceptionHandler`也可以用于使日志記錄更加健壯,而無需重新啟動線程,因為在線程執行失敗時,默認日志通常無法提供足夠的上下文信息。** **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看