Java Data Type Tutorial - Java ThreadGroup.destroy()








Syntax

ThreadGroup.destroy() has the following syntax.

public final void destroy()

Example

In the following code shows how to use ThreadGroup.destroy() method.

/*from  w  ww .  jav a2 s  .c om*/
public class Main {

  public static void main(String[] args) {
    ThreadGroupDemo tg = new ThreadGroupDemo();

  }
}

class ThreadGroupDemo implements Runnable {
  public ThreadGroupDemo() {
      try {     
         ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");
    
         ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

         Thread t1 = new Thread(pGroup, this);
         System.out.println("Starting " + t1.getName());
         t1.start();
            
         Thread t2 = new Thread(cGroup, this);
         System.out.println("Starting " + t2.getName());
         t2.start();

         // block until the other threads finish
         t1.join();
         t2.join();

         // child group destroyed
         cGroup.destroy();
         System.out.println(cGroup.getName() + " destroyed");
 
         // parent group destroyed
         pGroup.destroy();
         System.out.println(pGroup.getName() + " destroyed");

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

      System.out.println(Thread.currentThread().getName() + 
      " finished executing.");
   }
}

The code above generates the following result.