これは、JVM内で現在実行されているこれらのグループに属するすべてのスレッドグループとすべてのスレッドを表示することです。JVMで現在実行中のすべてのスレッドグループとスレッドを表示
スレッドグループが最初に表示され、このグループ内のすべてのトレッドが下に表示されるように出力する必要があります。これはすべてのスレッドグループに対して行われます。現在、私のコードはすべてのスレッドグループを表示し、すべてのスレッドを表示しますが、私が記述した出力に到達する方法は不明です。ここで
私の現在のコードです:
public ThreadGroup getThreadRoot() {
ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
ThreadGroup parentGroup;
while ((parentGroup = rootGroup.getParent()) != null) {
rootGroup = parentGroup;
}
return rootGroup;
}
public ThreadGroup[] getAllThreadGroups(){
ThreadGroup root= getThreadRoot();
int estimate = root.activeGroupCount();
ThreadGroup [] threads = new ThreadGroup[estimate];
while (root.enumerate(threads, true) == threads.length) {
threads = new ThreadGroup[ threads.length * 2 ];
}
ThreadGroup[] allGroups = new ThreadGroup[threads.length+1];
allGroups[0] = root;
System.arraycopy(threads, 0, allGroups, 1, estimate);
return allGroups;
}
public Thread[] getAllThreads(){
ThreadGroup root= getThreadRoot();
int estimate = root.activeGroupCount();
Thread [] allThreads = new Thread[estimate];
while (root.enumerate(allThreads, true) == allThreads.length) {
allThreads = new Thread[ allThreads.length * 2 ];
}
return allThreads;
}
と主な方法:すべての
public static void main(String[] args) {
CreateDummyGroups create = new CreateDummyGroups();
Functionality func = new Functionality();
ThreadGroup[] tg = func.getAllThreadGroups();
Thread[] t = func.getAllThreads();
for (int i=0; i<tg.length; i++) {
if(tg[i] != null){
System.out.println("Name: " + tg[i].getName());
}
}
for (int i=0; i<t.length; i++) {
if(t[i] != null){
System.out.println("Name: " + t[i].getName() + ", id: " + t[i].getId()
+ ", State: " + t[i].getState() + ", Is daemon? " + t[i].isDaemon());
}
}
}
}
すると自分の投稿をvandalizeないでください。 。それはよく書かれた、慎重に考え出された、正当な質問です。なぜそのコンテンツを削除しますか?さらに、Stack Exchangeはあなたが投稿したものすべてに対して完全な所有権を保持しているため、最初に自分の投稿を破壊することはできません。 – HyperNeutrino