2017-10-26 16 views
1

これは、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()); 
     } 
} 
} 

} 
+0

すると自分の投稿をvandalizeないでください。 。それはよく書かれた、慎重に考え出された、正当な質問です。なぜそのコンテンツを削除しますか?さらに、Stack Exchangeはあなたが投稿したものすべてに対して完全な所有権を保持しているため、最初に自分の投稿を破壊することはできません。 – HyperNeutrino

答えて

0

まず、すべてのスレッドグループとスレッドの階層的な出力を得るための最も簡単な解決のためには、 getThreadRoot()メソッドのみが必要です。

Functionality func = new Functionality(); 
func.getThreadRoot().list(); 

ただし、子としてのスレッドのみを持つグループのリストではなく、階層的なグループです。このために、あなただけの現在のグループのスレッドを表示するには、すなわち

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 j=0; j<t.length; j++) { 
      if(t[j] != null && t[j].getThreadGroup() == tg[i]) { 
      System.out.println(" Name: " + t[j].getName() + ", id: " + t[j].getId() 
        + ", State: " + t[j].getState() + ", Is daemon? " + t[j].isDaemon()); 
      } 
     } 
    } 
} 

これは内部ループ内の基準の比較を使用して、あなたのループを持っています。 Javaのため、

public Thread[] getThreadsOf(ThreadGroup group) { 
    int estimate = group.activeCount(); 
    Thread[] groupThreads = new Thread[estimate]; 
    while(group.enumerate(groupThreads, false) == groupThreads.length) { 
     groupThreads = new Thread[ groupThreads.length * 2 ]; 
    } 
    return groupThreads; 
} 

とところで

CreateDummyGroups create = new CreateDummyGroups(); 
Functionality func = new Functionality(); 
ThreadGroup[] tg = func.getAllThreadGroups(); 
for(int i=0; i<tg.length; i++) { 
    if(tg[i] != null) { 
     System.out.println("Name: " + tg[i].getName()); 
     Thread[] t = func.getThreadsOf(tg[i]); 
     for(int j=0; j<t.length; j++) { 
      if(t[j] != null) { 
      System.out.println(" Name: " + t[j].getName() + ", id: " + t[j].getId() 
        + ", State: " + t[j].getState() + ", Is daemon? " + t[j].isDaemon()); 
      } 
     } 
    } 
} 

に発信者を変更しますのみ、すなわち最初の場所で、現在のグループのスレッドを取得し、あなたのFunctionalityにメソッドを追加することですより効率的に5、これはうまく

CreateDummyGroups create = new CreateDummyGroups(); 
Functionality func = new Functionality(); 
for(ThreadGroup tg: func.getAllThreadGroups()) { 
    if(tg != null) { 
     System.out.println("Name: " + tg.getName()); 
     for(Thread t: func.getThreadsOf(tg)) { 
      if(t != null) { 
       System.out.println(" Name: " + t.getName() + ", id: " + t.getId() 
        + ", State: " + t.getState() + ", Is daemon? " + t.isDaemon()); 
      } 
     } 
    } 
} 

として書かしかし、これらの古いenumerate方法が非常に落胆していることに注意することができます。それらは使用するだけでなく、処理中にスレッドやグループが変更される可能性があるため、エラーが発生しやすくなります。それは時間の一点で行われた単一のスナップショット上で動作するように非常に簡単でより信頼性の高いです:

CreateDummyGroups create = new CreateDummyGroups(); 
Map<ThreadGroup, List<Thread>> map = new LinkedHashMap<>(); 
for(Thread thread: Thread.getAllStackTraces().keySet()) { 
    List<Thread> list = map.get(thread.getThreadGroup()); 
    if(list == null) { 
     list = new ArrayList<>(); 
     map.put(thread.getThreadGroup(), list); 
    } 
    list.add(thread); 
} 


for(Map.Entry<ThreadGroup,List<Thread>> groupEntry: map.entrySet()) { 
    System.out.println("Name: " + groupEntry.getKey().getName()); 
    for(Thread thread: groupEntry.getValue()) { 
     System.out.println(" Name: " + thread.getName() + ", id: " + thread.getId() 
      + ", State: " + thread.getState() + ", Is daemon? " + thread.isDaemon()); 
    } 
} 

のJava 8つの機能使用している場合、このロジックがさらに簡単になり:

CreateDummyGroups create = new CreateDummyGroups(); 
Map<ThreadGroup, List<Thread>> map = Thread.getAllStackTraces().keySet() 
    .stream().collect(Collectors.groupingBy(Thread::getThreadGroup)); 


map.forEach((group,threadList) -> { 
    System.out.println("Name: " + group.getName()); 
    threadList.forEach(thread -> 
     System.out.println(" Name: " + thread.getName() + ", id: " + thread.getId() 
      + ", State: " + thread.getState() + ", Is daemon? " + thread.isDaemon()) 
    ); 
}); 
関連する問題