Java ThreadGroup 类

说明

当该线程组中的线程由于未捕获的异常而停止时,Java 虚拟机将调用 Java ThreadGroup uncaughtException() 方法,并且该线程执行以下操作:没有安装特定的 Thread.UncaughtExceptionHandler。

声明

以下是 java.lang.ThreadGroup.uncaughtException() 方法的声明

public void uncaughtException(Thread t, Throwable e)

参数

  • t 这是即将退出的线程。

  • e 这是未捕获的异常。

返回值

此方法不返回任何值。

异常

示例 1

以下示例显示了 ThreadGroup uncaughtException() 方法在以下情况下的用法子级和孙级 ThreadGroup 对象。我们创建了一个新类 NewThreadGroup,它扩展了 ThreadGroup。它具有 uncaughtException() 方法的重写实现。使用 NewThreadGroup,我们创建一个线程组对象。然后,我们使用之前创建的子线程组对象和孙线程组对象创建了两个线程。线程启动后,我们使用 Interrupt() 方法调用来中断流程。一旦 InterruptedException 被处理,我们就会抛出一个 RuntimeException,然后最终由 uncaughtException() 方法处理。

package com.yxjc123;

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

   public void start() {
      try {     
         //创建ThreadGroup对象
    	 NewThreadGroup threadGroup = new NewThreadGroup("ThreadGroup");
		 
         //创建一个线程
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         //中断两个线程
         t1.interrupt();
         t2.interrupt();
         // 阻塞直到其他线程完成
         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());

         //重新抛出异常
         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

示例 2

以下示例显示了 ThreadGroup uncaughtException() 方法在子 ThreadGroup 和孙 ThreadGroup 对象的情况下的用法。我们创建了一个新类 NewThreadGroup,它扩展了 ThreadGroup。它具有 uncaughtException() 方法的重写实现。使用 NewThreadGroup,我们创建父、子线程组对象。然后,我们使用之前创建的子线程组对象和孙线程组对象创建了两个线程。线程启动后,我们使用 Interrupt() 方法调用来中断流程。一旦 InterruptedException 被处理,我们就会抛出一个 RuntimeException,然后最终由 uncaughtException() 方法处理。

package com.yxjc123;

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

   public void start() {
      try {     
         //创建父ThreadGroup
    	 NewThreadGroup pThreadGroup = new NewThreadGroup("Parent ThreadGroup");
		 
         //为父ThreadGroup创建子ThreadGroup
    	 NewThreadGroup cThreadGroup = new NewThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         //创建一个线程
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         //中断两个线程
         t1.interrupt();
         t2.interrupt();
         // 阻塞直到其他线程完成
         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());

         //重新抛出异常
         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

示例 3

以下示例显示了 ThreadGroup uncaughtException() 方法在子 ThreadGroup 和孙 ThreadGroup 对象的情况下的用法。我们创建了一个新类 NewThreadGroup,它扩展了 ThreadGroup。它具有 uncaughtException() 方法的重写实现。使用 NewThreadGroup,我们创建父线程组、子线程组和孙线程组对象。然后,我们使用之前创建的子线程组对象和孙线程组对象创建了两个线程。线程启动后,我们使用 Interrupt() 方法调用来中断流程。一旦 InterruptedException 被处理,我们就会抛出一个 RuntimeException,然后最终由 uncaughtException() 方法处理。

package com.yxjc123;

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

   public void start() {
      try {     
         //创建父ThreadGroup
    	 NewThreadGroup pThreadGroup = new NewThreadGroup("Parent ThreadGroup");
		 
         //为父ThreadGroup创建子ThreadGroup
    	 NewThreadGroup cThreadGroup = new NewThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         //为父ThreadGroup创建孙ThreadGroup
    	 NewThreadGroup gThreadGroup = new NewThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         //中断两个线程
         t1.interrupt();
         t2.interrupt();
         // 阻塞直到其他线程完成
         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());

         //重新抛出异常
         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