Showing posts with label thread group example. Show all posts
Showing posts with label thread group example. Show all posts

Thursday, 6 March 2014

Destroy Thread group

ThreadGroup class provides destroy method, to destroy a ThreadGroup, if the thread group empty. Empty mean, it don't have any active threads and all the sub groups also empty.

public final void destroy()
Destroys this thread group and all of its subgroups. This thread group must be empty, indicating that all threads that had been in this thread group have since stopped.

class GroupDestroyEx extends Thread{

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

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

  GroupDestroyEx thread1 = new GroupDestroyEx(group1, "thread1");
  GroupDestroyEx thread2 = new GroupDestroyEx(group1, "thread2");

  thread1.start();
  thread2.start();

  thread1.join();
  thread2.join();

  group1.destroy();
  System.out.println("Is group1 destroyed " + group1.isDestroyed());
  System.out.println("Is group1 destroyed " + group2.isDestroyed());
 }
}
   
Output
Is group1 destroyed true
Is group1 destroyed true

Some Points to Remember
1. IllegalThreadStateException thrown, when you tries to destroy a group, which has some active threads or active sub groups in it.
class GroupDestroyEx extends Thread{

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

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

  GroupDestroyEx thread1 = new GroupDestroyEx(group1, "thread1");
  GroupDestroyEx thread2 = new GroupDestroyEx(group1, "thread2");

  thread1.start();
  thread2.start();

  group1.destroy();

  System.out.println("Is group1 destroyed " + group1.isDestroyed());
  System.out.println("Is group1 destroyed " + group2.isDestroyed());
 }
}
   
Output
Exception in thread "main" java.lang.IllegalThreadStateException
  at java.lang.ThreadGroup.destroy(Unknown Source)
  at GroupDestroyEx.main(GroupDestroyEx.java:26)
   



Daemon Thread Group                                                 TimeUnit Class                                                 Home

Wednesday, 5 March 2014

Daemon Thread Group

public final void setDaemon(boolean daemon)
If the parameter daemon is set to true, then changes the daemon status of this thread group. A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.

If the thread group is not daemon, then the group is not destroyed even when its last thread stopped. A daemon thread group can contain daemon and Non daemon threads in it.

Example
class DaemonGroupEx extends Thread{
 DaemonGroupEx(ThreadGroup grp, String name){
  super(grp, name);
 }

 public void run(){
  Thread local = Thread.currentThread();
  System.out.println(local.getName() + " is daemon " + local.isDaemon());
 }

 public static void main(String args[])throws Exception{
  ThreadGroup group1 = new ThreadGroup("group1");
  DaemonGroupEx t1 = new DaemonGroupEx(group1, "thread1");
  t1.start();
  t1.join();
  System.out.println("Is group1 destroyed " + group1.isDestroyed());
 }
}

Output
thread1 is daemon false
Is group1 destroyed false

A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.

Example
class DaemonGroupEx extends Thread{
 DaemonGroupEx(ThreadGroup grp, String name){
  super(grp, name);
 }

 public void run(){
  Thread local = Thread.currentThread();
  System.out.println(local.getName() + " is daemon " + local.isDaemon());
 }

 public static void main(String args[])throws Exception{
  ThreadGroup group1 = new ThreadGroup("group1");
  group1.setDaemon(true);

  DaemonGroupEx t1 = new DaemonGroupEx(group1, "thread1");
  t1.start();
  t1.join();

  System.out.println("Is group1 destroyed " + group1.isDestroyed());
 }
}

Output  
thread1 is daemon false
Is group1 destroyed true

   
Related Posts



Interrupt Thread Group                                                 Destroy Thread Group                                                 Home

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