Tuesday, 4 March 2014

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

No comments:

Post a Comment