Java ThreadGroup 类

描述

Java ThreadGroup destroy() 方法销毁该线程组及其所有子组。该线程组必须为空,表明该线程组中的所有线程已停止。

声明

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

public final void destroy()

参数

返回值

此方法不返回任何值。

异常

  • IllegalThreadStateException 如果线程组不为空或者线程组已经存在

  • SecurityException 如果当前线程无法修改此线程组。

示例 1

以下示例显示了在单个 ThreadGroup 对象的情况下 ThreadGroup destroy() 方法的用法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。然后我们使用之前创建的线程组对象创建了两个线程。使用 destroy() 方法,一旦线程执行,我们就会销毁 ThreadGroup 对象。

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
         ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         // 阻塞直到其他线程完成
         t1.join();
         t2.join();
            
         //销毁线程组。
         threadGroup.destroy();
         System.out.println(threadGroup.getName() + " destroyed." );

      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
            Thread.sleep(50);
         } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
         }
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

输出

让我们编译并运行上面的程序,这将产生以下结果

Starting Thread-0...
Starting Thread-1...
Thread-0 finished executing.
Thread-1 finished executing.
ThreadGroup destroyed.

示例 2

以下示例显示了在多个 ThreadGroup 对象的情况下 ThreadGroup destroy() 方法的用法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后我们使用之前创建的线程组对象创建了两个线程。使用 destroy() 方法,一旦线程执行,我们就会销毁 ThreadGroup 对象。

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
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         //为父ThreadGroup创建子ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // 阻塞直到其他线程完成
         t1.join();
         t2.join();

         //销毁线程组。
         cThreadGroup.destroy();
         System.out.println(cThreadGroup.getName() + " destroyed." );
         pThreadGroup.destroy();
         System.out.println(pThreadGroup.getName() + " destroyed." );

      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
            Thread.sleep(50);
         } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
         }
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

输出

让我们编译并运行上面的程序,这将产生以下结果

Starting Thread-0...
Starting Thread-1...
Thread-0 finished executing.
Thread-1 finished executing.
Child ThreadGroup destroyed.
Parent ThreadGroup destroyed.

示例 3

以下示例显示 ThreadGroup destroy() 方法的无效使用。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后我们使用之前创建的线程组对象创建了两个线程。使用 destroy() 方法,我们试图在线程执行之前销毁 ThreadGroup 对象。

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
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         //为父ThreadGroup创建子ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");

         //创建一个线程
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         //创建另一个线程
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         //销毁线程组。
         cThreadGroup.destroy();
         System.out.println(cThreadGroup.getName() + " destroyed." );

         pThreadGroup.destroy();
         System.out.println(pThreadGroup.getName() + " destroyed." );
 
         // 阻塞直到其他线程完成
         t1.join();
         t2.join();

      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
         } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
         }
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

输出

让我们编译并运行上面的程序,这将产生以下结果 -

Starting Thread-0...
Starting Thread-1...
Exception in thread "main" java.lang.IllegalThreadStateException
	at java.lang.ThreadGroup.destroy(Unknown Source)
	at com.yxjc123.ThreadGroupDemo.start(ThreadGroupDemo.java:28)
	at com.yxjc123.ThreadGroupDemo.main(ThreadGroupDemo.java:6)
Thread-0 finished executing.
Thread-1 finished executing.