Java - ThreadGroup destroy() Method



Description

The Java ThreadGroup destroy() method destroys this thread group and all of its subgroups. This thread group must be empty, indicating that all threads that had been in this thread group have since stopped.

Declaration

Following is the declaration for java.lang.ThreadGroup.destroy() method

public final void destroy()

Parameters

NA

Return Value

This method does not return any value.

Exception

  • IllegalThreadStateException − if the thread group is not empty or if the thread group has already been destroyed.

  • SecurityException − if the current thread cannot modify this thread group.

Example 1

The following example shows the usage of ThreadGroup destroy() method in case of a single ThreadGroup object. We've created a ThreadGroup object and assigned it a name. Then we've created two threads using the threadgroup object created earlier. Using destroy() method, we're destroying the ThreadGroup object once threads are executed.

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.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

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

Example 2

The following example shows the usage of ThreadGroup destroy() method in case of multiple ThreadGroup objects. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Then we've created two threads using the threadgroup objects created earlier. Using destroy() method, we're destroying the ThreadGroup objects once threads are executed.

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.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

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

Example 3

The following example shows an invalid use of ThreadGroup destroy() method. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Then we've created two threads using the threadgroup objects created earlier. Using destroy() method, we're trying to destroy the ThreadGroup objects even before threads are executed.

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.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

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
Advertisements