Java - ThreadGroup enumerate(ThreadGroup[] list) Method



Description

The Java ThreadGroup enumerate(ThreadGroup[] list) method copies into the specified array references to every active subgroup in this thread group.

Declaration

Following is the declaration for java.lang.ThreadGroup.enumerate(ThreadGroup[] list) method

public int enumerate(ThreadGroup[] list)

Parameters

list − This is an array into which to place the list of thread groups.

Return Value

This method returns the number of thread groups put into the array.

Exception

SecurityException − if the current thread does not have permission to enumerate this thread group.

Example 1

The following example shows the usage of ThreadGroup enumerate(ThreadGroup[] list) 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 enumerate() method, we're getting all the sub thread groups into an array and then we're printing their names using for loop on the array. As there is no subgroup, count will be zero.

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();

         // returns the number of thread groups put into the array
         ThreadGroup[] grpList = new ThreadGroup[threadGroup.activeGroupCount()];
         int count = threadGroup.enumerate(grpList);
         for (int i = 0; i < count; i++) {
            System.out.println("Thread " + grpList[i].getName() + " found.");
         }

         // 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...
Thread-1 finished executing.
Thread-0 finished executing.

Example 2

The following example shows the usage of ThreadGroup enumerate(ThreadGroup[] list) 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 enumerate() method, we're getting all the sub thread group from parent ThreadGroup object into an array and then we're printing their names using for loop on the array.

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();

         // returns the number of thread groups put into the array
         ThreadGroup[] grpList = new ThreadGroup[pThreadGroup.activeGroupCount()];
         int count = pThreadGroup.enumerate(grpList);
         for (int i = 0; i < count; i++) {
            System.out.println("ThreadGroup " + grpList[i].getName() + " found.");
         }
            
         // 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...
ThreadGroup Child ThreadGroup found.
Thread-0 finished executing.
Thread-1 finished executing.

Example 3

The following example shows the usage of ThreadGroup enumerate(ThreadGroup[] list) method in case of child and grandchild 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 child and grandchild threadgroup objects created earlier. Using enumerate() method, we're getting all the active threads into an array from the grandparent ThreadGroup object and then we're printing their names using for loop on the array.

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 grandchild ThreadGroup for parent ThreadGroup
         ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         // create a thread
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // returns the number of thread groups put into the array
         ThreadGroup[] grpList = new ThreadGroup[pThreadGroup.activeGroupCount()];
         int count = pThreadGroup.enumerate(grpList);
         for (int i = 0; i < count; i++) {
            System.out.println("ThreadGroup " + grpList[i].getName() + " found.");
         }

         // 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...
ThreadGroup Child ThreadGroup found.
ThreadGroup GrandChild ThreadGroup found.
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
Advertisements