Created
August 7, 2012 18:19
-
-
Save pk11/3288020 to your computer and use it in GitHub Desktop.
print current threads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void printThreads() { | |
// Find the root thread group | |
ThreadGroup root = Thread.currentThread().getThreadGroup().getParent(); | |
while (root.getParent() != null) { | |
root = root.getParent(); | |
} | |
// Visit each thread group | |
visit(root, 0); | |
} | |
// This method recursively visits all thread groups under `group'. | |
private static void visit(ThreadGroup group, int level) { | |
// Get threads in `group' | |
int numThreads = group.activeCount(); | |
Thread[] threads = new Thread[numThreads*2]; | |
numThreads = group.enumerate(threads, false); | |
// Enumerate each thread in `group' | |
for (int i=0; i<numThreads; i++) { | |
System.out.println(threads[i].toString()); | |
} | |
// Get thread subgroups of `group' | |
int numGroups = group.activeGroupCount(); | |
ThreadGroup[] groups = new ThreadGroup[numGroups*2]; | |
numGroups = group.enumerate(groups, false); | |
// Recursively visit each subgroup | |
for (int i=0; i<numGroups; i++) { | |
visit(groups[i], level+1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment