I was going though ThreadGroups in java. As per the Javadoc, it is written that,
A thread is allowed to access information about its own thread group but not to access information about its thread group’s parent thread group or any other thread group.
But when I implemented the following code it was working,
public static void main(String args[]){
//parent thread group It_Firm
ThreadGroup It_Firm=new ThreadGroup("It_Firm");
//Child thread group web
ThreadGroup web=new ThreadGroup(It_Firm,"webdeveloper");
/*
* A thread entry in child thread group set in which i am trying to call parent's thread group activecount()
* method,as per the docs it will stop me to call for any information from parent's thread group or any other
* thread group but it is not doing it.
*/
Thread th=new Thread(web,new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
Thread ths[]=new Thread[Thread.currentThread().getThreadGroup().getParent().activeCount()];
Thread.currentThread().getThreadGroup().getParent().enumerate(ths);
for(int i=0;i<ths.length;i++){
System.out.println("group name"+ths[i].getThreadGroup().getName()+" : name : "+ths[i].getName());
System.out.println("state"+ths[i].isAlive());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
},"prashank");
th.start();
//some dummy code in parent thread group
Thread th_pthread=new Thread(It_Firm,new Runnable(){
@Override
public void run() {
boolean flag=true;
while(flag){
Scanner sc=new Scanner(System.in);
char ch=sc.nextLine().charAt(0);
if(ch=='N')
flag=false;
}
}
},"abc pvt ltd");
th_pthread.start();
}
Now I can't understand what is happening, I am new to this, why i am able to get information about current thread's thread group's parent. Am i missing something,any information on this?