Wednesday 5 March 2014

enumerate : find sub groups of thread group

Thread Group class provides two methods to find the sub groups of a particular group

1. public int enumerate(ThreadGroup[] list)
Copies into the specified array references to every active subgroup in this thread group and its subgroups recursively. Returns the number of thread groups put into the array.

Example
class GroupEx{
 public static void main(String args[]){
  ThreadGroup group1 = new ThreadGroup("group1");
  ThreadGroup group2 = new ThreadGroup(group1, "group2");
  ThreadGroup group3 = new ThreadGroup(group2, "group3");
  ThreadGroup group4 = new ThreadGroup(group2, "group4");

  int noOfGroups = group1.activeGroupCount();
  ThreadGroup allGroups[] = new ThreadGroup[noOfGroups];
  group1.enumerate(allGroups);

  System.out.println("group1 has " +noOfGroups + " sub groups");
  System.out.println("Sub groups in the group1 are ");

  for(int i=0; i < allGroups.length; i++){
   System.out.println(allGroups[i].getName());
  }
 }
}

Output   
group1 has 3 sub groups
Sub groups in the group1 are
group2
group3
group4

2. public int enumerate(ThreadGroup[] list, boolean recurse)
Copies into the specified array references to every active subgroup in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group and references to every active thread group in these subgroups are also included. Returns the number of thread groups put into the array.

Example1 : Recursively finds the sub groups
class GroupEx{
 public static void main(String args[]){
  ThreadGroup group1 = new ThreadGroup("group1");
  ThreadGroup group2 = new ThreadGroup(group1, "group2");
  ThreadGroup group3 = new ThreadGroup(group2, "group3");
  ThreadGroup group4 = new ThreadGroup(group2, "group4");

  int noOfGroups = group1.activeGroupCount();
  ThreadGroup allGroups[] = new ThreadGroup[noOfGroups];
  group1.enumerate(allGroups, true);

  System.out.println("group1 has " +noOfGroups + " sub groups");
  System.out.println("Sub groups in the group1 are ");

  for(int i=0; i < allGroups.length; i++){
   System.out.println(allGroups[i].getName());
  }
 }
}
   
Output
group1 has 3 sub groups
Sub groups in the group1 are
group2
group3
group4

Example2 : Finds the sub groups with out recursive travels
class GroupEx{
 public static void main(String args[]){
  ThreadGroup group1 = new ThreadGroup("group1");
  ThreadGroup group2 = new ThreadGroup(group1, "group2");
  ThreadGroup group3 = new ThreadGroup(group2, "group3");
  ThreadGroup group4 = new ThreadGroup(group2, "group4");

  int noOfGroups = group1.activeGroupCount();
  ThreadGroup allGroups[] = new ThreadGroup[noOfGroups];
  int length = group1.enumerate(allGroups, false);

  System.out.println("group1 has " +noOfGroups + " sub groups");
  System.out.println("Sub groups in the group1 are ");

  for(int i=0; i < length; i++){
   System.out.println(allGroups[i].getName());
  }
 }
}

Output
group1 has 3 sub groups
Sub groups in the group1 are
group2

Some Points to Remember
1. Invocation of enumerate(list) exactly behaves as enumerate(list, true)
  


Number Of Sub groups                                                 Active threads in a group                                                 Home

No comments:

Post a Comment