Java - ThreadGroup uncaughtException() 方法



描述

當此執行緒組中的執行緒由於未捕獲的異常而停止,並且該執行緒未安裝特定的 Thread.UncaughtExceptionHandler 時,Java ThreadGroup uncaughtException() 方法由 Java 虛擬機器呼叫。

宣告

以下是java.lang.ThreadGroup.uncaughtException() 方法的宣告

public void uncaughtException(Thread t, Throwable e)

引數

  • t − 即將退出的執行緒。

  • e − 未捕獲的異常。

返回值

此方法不返回任何值。

異常

ThreadGroup 物件中檢查未處理異常的示例

以下示例演示了在 ThreadGroup 物件的情況下使用 ThreadGroup uncaughtException() 方法。我們建立了一個新類 NewThreadGroup,它擴充套件了 ThreadGroup。它具有 uncaughtException() 方法的重寫實現。使用 NewThreadGroup,我們正在建立一個執行緒組物件。然後,我們使用前面建立的子執行緒組和孫執行緒組物件建立了兩個執行緒。啟動執行緒後,我們使用 interrupt() 方法呼叫來中斷流程。處理 InterruptedException 後,我們將丟擲一個 RuntimeException,最終由 uncaughtException() 方法處理。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a ThreadGroup object
    	 NewThreadGroup threadGroup = new NewThreadGroup("ThreadGroup");
		 
         // create a thread
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         // interrupt the two threads
         t1.interrupt();
         t2.interrupt();
         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   public void run() {
      try {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" executing...");

         while(true) {
            Thread.sleep(500);
         }
      } catch(InterruptedException e) {
         Thread currThread = Thread.currentThread();
         System.out.print(currThread.getName());
         System.out.println(" interrupted:" + e.toString());

         // rethrow the exception
         throw new RuntimeException(e.getMessage());
      }
   } 
}

class NewThreadGroup extends ThreadGroup {

   NewThreadGroup(String n) {
      super(n);
   }

   NewThreadGroup(ThreadGroup parent, String n) {
      super(parent, n);
   }

   public void uncaughtException(Thread t, Throwable e) {
      System.out.println(t + " has unhandled exception:" + e);
   } 
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread[Thread-0,5,ThreadGroup]...
Starting Thread[Thread-1,5,ThreadGroup]...
Thread-0 executing...
Thread-1 executing...
Thread-1Thread-0 interrupted:java.lang.InterruptedException: sleep interrupted
 interrupted:java.lang.InterruptedException: sleep interrupted
Thread[Thread-0,5,ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
Thread[Thread-1,5,ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted

子 ThreadGroup 物件中檢查未處理異常的示例

以下示例演示了在父和子 ThreadGroup 物件的情況下使用 ThreadGroup uncaughtException() 方法。我們建立了一個新類 NewThreadGroup,它擴充套件了 ThreadGroup。它具有 uncaughtException() 方法的重寫實現。使用 NewThreadGroup,我們正在建立父、子執行緒組物件。然後,我們使用前面建立的子執行緒組和孫執行緒組物件建立了兩個執行緒。啟動執行緒後,我們使用 interrupt() 方法呼叫來中斷流程。處理 InterruptedException 後,我們將丟擲一個 RuntimeException,最終由 uncaughtException() 方法處理。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
    	 NewThreadGroup pThreadGroup = new NewThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
    	 NewThreadGroup cThreadGroup = new NewThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         // interrupt the two threads
         t1.interrupt();
         t2.interrupt();
         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   public void run() {
      try {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" executing...");

         while(true) {
            Thread.sleep(500);
         }
      } catch(InterruptedException e) {
         Thread currThread = Thread.currentThread();
         System.out.print(currThread.getName());
         System.out.println(" interrupted:" + e.toString());

         // rethrow the exception
         throw new RuntimeException(e.getMessage());
      }
   } 
}

class NewThreadGroup extends ThreadGroup {

   NewThreadGroup(String n) {
      super(n);
   }

   NewThreadGroup(ThreadGroup parent, String n) {
      super(parent, n);
   }

   public void uncaughtException(Thread t, Throwable e) {
      System.out.println(t + " has unhandled exception:" + e);
   } 
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread[Thread-0,5,Parent ThreadGroup]...
Starting Thread[Thread-1,5,Child ThreadGroup]...
Thread-0 executing...
Thread-1 executing...
Thread-1Thread-0 interrupted:java.lang.InterruptedException: sleep interrupted
 interrupted:java.lang.InterruptedException: sleep interrupted
Thread[Thread-0,5,Parent ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
Thread[Thread-1,5,Child ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted

子執行緒組和孫執行緒組物件中檢查未處理異常的示例

以下示例演示了在子執行緒組和孫執行緒組物件的情況下使用 ThreadGroup uncaughtException() 方法。我們建立了一個新類 NewThreadGroup,它擴充套件了 ThreadGroup。它具有 uncaughtException() 方法的重寫實現。使用 NewThreadGroup,我們正在建立父、子執行緒組和孫執行緒組物件。然後,我們使用前面建立的子執行緒組和孫執行緒組物件建立了兩個執行緒。啟動執行緒後,我們使用 interrupt() 方法呼叫來中斷流程。處理 InterruptedException 後,我們將丟擲一個 RuntimeException,最終由 uncaughtException() 方法處理。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
    	 NewThreadGroup pThreadGroup = new NewThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
    	 NewThreadGroup cThreadGroup = new NewThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         // create a grandchild ThreadGroup for parent ThreadGroup
    	 NewThreadGroup gThreadGroup = new NewThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         // create a thread
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         // interrupt the two threads
         t1.interrupt();
         t2.interrupt();
         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   public void run() {
      try {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" executing...");

         while(true) {
            Thread.sleep(500);
         }
      } catch(InterruptedException e) {
         Thread currThread = Thread.currentThread();
         System.out.print(currThread.getName());
         System.out.println(" interrupted:" + e.toString());

         // rethrow the exception
         throw new RuntimeException(e.getMessage());
      }
   } 
}

class NewThreadGroup extends ThreadGroup {

   NewThreadGroup(String n) {
      super(n);
   }

   NewThreadGroup(ThreadGroup parent, String n) {
      super(parent, n);
   }

   public void uncaughtException(Thread t, Throwable e) {
      System.out.println(t + " has unhandled exception:" + e);
   } 
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread[Thread-0,5,Child ThreadGroup]...
Starting Thread[Thread-1,5,GrandChild ThreadGroup]...
Thread-0 executing...
Thread-1 executing...
Thread-1Thread-0 interrupted:java.lang.InterruptedException: sleep interrupted
 interrupted:java.lang.InterruptedException: sleep interrupted
Thread[Thread-1,5,GrandChild ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
Thread[Thread-0,5,Child ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
java_lang_threadgroup.htm
廣告