私はJavaでThreadGroupsを行っていました。Javadoc によると、次のように書かれています。
スレッドは、自身のスレッド グループに関する情報にアクセスできますが、そのスレッド グループの親スレッド グループまたはその他のスレッド グループに関する情報にアクセスすることはできません。
しかし、次のコードを実装したところ、機能していました。
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();
}
現在、何が起こっているのか理解できません。これは初めてです。現在のスレッドのスレッドグループの親に関する情報を取得できるのはなぜですか。何か不足していますか、これに関する情報はありますか?