Wednesday, 5 March 2014

enumerate : Active Threads of group

ThreadGroup class provides 2 methods to find out the Active thread in a group and its sub groups also.

1. public int enumerate(Thread[] list)
Copies into the specified array every active thread in this thread group and its subgroups. Returns the number of threads put into the array

class GroupEx extends Thread{

 public void run(){
  try{
   Thread.currentThread().sleep(2000);
  }
  catch(Exception e){
  }
 }

 GroupEx(ThreadGroup grp, String name){
  super(grp, name);
 }

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

  GroupEx t1 = new GroupEx(group1, "Thread1Grp1");
  GroupEx t2 = new GroupEx(group2, "Thread2Grp2");
  GroupEx t3 = new GroupEx(group3, "Thread3Grp3");
  GroupEx t4 = new GroupEx(group4, "Thread4Grp4");

  t1.start();
  t2.start();
  t3.start();
  t4.start();

  int noOfThreads = group1.activeCount();
  Thread allThreads[] = new Thread[noOfThreads];
  int threadCount = group1.enumerate(allThreads);

  System.out.println("Active threads in the group1 are " + threadCount);

  for(int i=0; i < threadCount; i++){
   System.out.println(allThreads[i].getName());
  }
 }
}
  
  
Output
Active threads in the group1 are 4
Thread1Grp1
Thread2Grp2
Thread3Grp3
Thread4Grp4

2. public int enumerate(Thread[] list, boolean recurse)
Copies into the specified array every active thread in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group and references to every active thread in these subgroups are also included. If the array is too short to hold all the threads, the extra threads are silently ignored. Returns the number of threads put into the array.

Example 1
class GroupEx extends Thread{

 public void run(){
  try{
   Thread.currentThread().sleep(2000);
  }
  catch(Exception e){
  }
 }

 GroupEx(ThreadGroup grp, String name){
  super(grp, name);
 }

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

  GroupEx t1 = new GroupEx(group1, "Thread1Grp1");
  GroupEx t2 = new GroupEx(group2, "Thread2Grp2");
  GroupEx t3 = new GroupEx(group3, "Thread3Grp3");
  GroupEx t4 = new GroupEx(group4, "Thread4Grp4");

  t1.start();
  t2.start();
  t3.start();
  t4.start();

  int noOfThreads = group1.activeCount();
  Thread allThreads[] = new Thread[noOfThreads];
  int threadCount = group1.enumerate(allThreads, false);

  System.out.println("Active threads in the group1 are " + threadCount);

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

Output
Active threads in the group1 are 1
Thread1Grp1

Example 2
class GroupEx extends Thread{
  
 public void run(){
  try{
   Thread.currentThread().sleep(2000);
  }
  catch(Exception e){
  }
 }

 GroupEx(ThreadGroup grp, String name){
  super(grp, name);
 }

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

  GroupEx t1 = new GroupEx(group1, "Thread1Grp1");
  GroupEx t2 = new GroupEx(group2, "Thread2Grp2");
  GroupEx t3 = new GroupEx(group3, "Thread3Grp3");
  GroupEx t4 = new GroupEx(group4, "Thread4Grp4");

  t1.start();
  t2.start();
  t3.start();
  t4.start();

  int noOfThreads = group1.activeCount();
  Thread allThreads[] = new Thread[noOfThreads];
  int threadCount = group1.enumerate(allThreads, true);

  System.out.println("Active threads in the group1 are " + threadCount);

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

Output   
Active threads in the group1 are 4
Thread1Grp1
Thread2Grp2
Thread3Grp3
Thread4Grp4
  
      

Find Sub Groups                                                 Set Maximum priority                                                 Home

No comments:

Post a Comment