Wednesday, 5 March 2014

ThreadGroup interrupt

ThreadGroup class provides interrupt method, to interrupt all the threads in a group.

public final void interrupt()
Interrupts all threads in this thread group and its sub groups.

Example
class InterruptEx extends Thread{
 public void run(){
  while(!isInterrupted()){
  }
  System.out.println(Thread.currentThread().getName() + " Interrupted");
 }

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

 public static void main(String args[])throws Exception{
  ThreadGroup group1 = new ThreadGroup("group1");
  ThreadGroup group2 = new ThreadGroup(group1, "group2");

  InterruptEx t1 = new InterruptEx(group1, "Thread1Group1");
  InterruptEx t2 = new InterruptEx(group1, "Thread2Group1");
  InterruptEx t3 = new InterruptEx(group1, "Thread3Group2");
  InterruptEx t4 = new InterruptEx(group1, "Thread4Group2"); 
 
  t1.start();
  t2.start();
  t3.start();
  t4.start();

  group1.interrupt();

  t1.join();
  t2.join();
  t3.join();
  t4.join();
 }
}






Get maximum priority                                                 Daemon Thread Group                                                 Home

getMaxPriority : get priority of thread group

public final int getMaxPriority()
Returns the maximum priority of this thread group. Threads that are part of this group cannot have a higher priority than the maximum priority.

class PriorityEx extends Thread{
 public void run(){
  try{
   Thread.currentThread().sleep(1000);
   Thread local = Thread.currentThread();
   System.out.println(local.getName() + " priority is " + local.getPriority());
  }
  catch(InterruptedException e){
  }
 }

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

 public static void main(String args[]){
  ThreadGroup group1 = new ThreadGroup("group1");

  PriorityEx t1 = new PriorityEx(group1, "Thread1");
  t1.setPriority(9);
  t1.start();

  group1.setMaxPriority(8);
  System.out.println("Group1 priority is set to " + group1.getMaxPriority());

  PriorityEx t2 = new PriorityEx(group1, "Thread2");
  t2.setPriority(9);
  t2.start();
 }
}

Output
Group1 priority is set to 8
Thread1 priority is 9
Thread2 priority is 8
  
Group1 maximum priority is set to 9, but thread t1 set to priority 9, before setting thr grup1 priority, so it is no affected. But Thread t2 trying to set priority of 9, after setting the group maximum priority of 8, So Thread t2 limited to its priority 8.

Related Posts



Set maximum Priority                                                 Interrupt Thread Group                                                 Home

setMaxPriority : set priority to thread group

ThreadGroup class provides a method to set the Max priorities to all the threads in the group.

public final void setMaxPriority(int pri)
Sets the maximum priority of the group. Threads in the thread group that already have a higher priority are not affected. Threads that are going to add this group, after setting the priority to a group, have the maximum priority less than or equal to the group maximum priority. Setting the thread priority to more than the group maximum priority limits the thread priority to group maximum priority.

Example
class PriorityEx extends Thread{
 public void run(){
  try{
   Thread.currentThread().sleep(1000);
   Thread local = Thread.currentThread();
   System.out.println(local.getName() + " priority is " + local.getPriority());
  }
  catch(InterruptedException e){
  }
 }

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

 public static void main(String args[]){
  ThreadGroup group1 = new ThreadGroup("group1");

  PriorityEx t1 = new PriorityEx(group1, "Thread1");
  t1.setPriority(9);
  t1.start();

  group1.setMaxPriority(8);
  System.out.println("Group1 priority is set to " + group1.getMaxPriority());

  PriorityEx t2 = new PriorityEx(group1, "Thread2");
  t2.setPriority(9);
  t2.start();
 }
}
 

Output
Group1 priority is set to 8
Thread1 priority is 9
Thread2 priority is 8
   
Group1 maximum priority is set to 9, but thread t1 set to priority 9, before setting group1 priority, so it is not affected. But Thread t2 trying to set priority of 9, after setting the group maximum priority of 8, So Thread t2 limited to its priority 8.   



Active Threads of Group                                                 Get Maximum priority                                                 Home

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

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

Tuesday, 4 March 2014

activeGroupCount() : Find number of subgroups

public int activeGroupCount()
Returns an estimate of the number of active groups in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group. The value returned by this method is an estimate only, since groups are added and removed dynamically.

Example
class GroupCount{
 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, "group3");

  System.out.println("Active number of groups in group1 is " + group1.activeGroupCount());
  System.out.println("Active number of groups in group2 is " + group2.activeGroupCount());
  System.out.println("Active number of groups in group3 is " + group3.activeGroupCount());
  System.out.println("Active number of groups in group4 is " + group4.activeGroupCount());
 }
}
   
Output

Active number of groups in group1 is 3
Active number of groups in group2 is 2
Active number of groups in group3 is 0
Active number of groups in group4 is 0



Get parent thread group                                                 Find sub groups                                                 Home

getParent: Get the Parent Group

public final ThreadGroup getParent()
Returns the parent of this thread group.

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

  System.out.println("Parent group for group1 is " + group1.getParent());
  System.out.println("Parent group for group2 is " + group2.getParent());
  System.out.println("Parent group for group3 is " + group3.getParent());
 }
}
   
Output
Parent group for group1 is java.lang.ThreadGroup[name=main,maxpri=10]
Parent group for group2 is java.lang.ThreadGroup[name=group1,maxpri=10]
Parent group for group3 is java.lang.ThreadGroup[name=group2,maxpri=10]

main thread is in the main thread group, which is part of system thread group. The system thread group is the root group, so the parent group for system group is null.

public class GetParent {
 public static void main(String args[]){
  Thread mainThread = Thread.currentThread();

  System.out.println(mainThread.getThreadGroup());
  System.out.println(mainThread.getThreadGroup().getParent());
  System.out.println(mainThread.getThreadGroup().getParent().getParent());
 }
}
   
Output
java.lang.ThreadGroup[name=main,maxpri=10]
java.lang.ThreadGroup[name=system,maxpri=10]
null


Add thread to group                                                 Active group count                                                 Home