Java - ThreadGroup destroy() 方法



描述

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

声明

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

public final void destroy()

参数

返回值

此方法不返回任何值。

异常

  • IllegalThreadStateException − 如果线程组不为空,或者线程组已被销毁。

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

销毁单个ThreadObject对象的示例

以下示例展示了在单个ThreadGroup对象的情况下使用ThreadGroup destroy()方法的情况。我们创建了一个ThreadGroup对象并为其命名。然后,我们使用前面创建的threadgroup对象创建了两个线程。使用destroy()方法,一旦线程执行完毕,我们就销毁ThreadGroup对象。

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

         // create a thread
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         // block until the other threads finish
         t1.join();
         t2.join();
            
         // destroy the ThreadGroup.
         threadGroup.destroy();
         System.out.println(threadGroup.getName() + " destroyed." );

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

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			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.

在多个ThreadGroup对象中销毁ThreadObject对象的示例

以下示例展示了在多个ThreadGroup对象的情况下使用ThreadGroup destroy()方法的情况。我们创建了一个ThreadGroup对象并为其命名。接下来,我们创建了一个子ThreadGroup对象。然后,我们使用前面创建的threadgroup对象创建了两个线程。使用destroy()方法,一旦线程执行完毕,我们就销毁ThreadGroup对象。

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
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");

         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // block until the other threads finish
         t1.join();
         t2.join();

         // destroy the ThreadGroups.
         cThreadGroup.destroy();
         System.out.println(cThreadGroup.getName() + " destroyed." );

         pThreadGroup.destroy();
         System.out.println(pThreadGroup.getName() + " destroyed." );

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

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			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.

在销毁ThreadObject对象时遇到异常的示例

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

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
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");

         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // destroy the ThreadGroups.
         cThreadGroup.destroy();
         System.out.println(cThreadGroup.getName() + " destroyed." );

         pThreadGroup.destroy();
         System.out.println(pThreadGroup.getName() + " destroyed." );
 
         // block until the other threads finish
         t1.join();
         t2.join();

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

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			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.tutorialspoint.ThreadGroupDemo.start(ThreadGroupDemo.java:28)
	at com.tutorialspoint.ThreadGroupDemo.main(ThreadGroupDemo.java:6)
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
广告