Java Data Type Tutorial - Java ThreadGroup .enumerate (ThreadGroup [] list, boolean recurse)








Syntax

ThreadGroup.enumerate(ThreadGroup [] list, boolean recurse) has the following syntax.

public int enumerate(ThreadGroup [] list,  boolean recurse)

Example

In the following code shows how to use ThreadGroup.enumerate(ThreadGroup [] list, boolean recurse) method.

public class Main {
  public static void main(String[] args) {
    ThreadGroupDemo tg = new ThreadGroupDemo();
//from w  w  w. ja va 2 s .c om
  }

}

class ThreadGroupDemo implements Runnable {

  public ThreadGroupDemo() {
    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();

    ThreadGroup[] grpList = new ThreadGroup[pGroup.activeGroupCount()];
    int count = pGroup.enumerate(grpList, true);
    for (int i = 0; i < count; i++) {
      System.out.println("ThreadGroup" + grpList[i].getName() + " found");
    }
  }

  public void run() {

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

The code above generates the following result.