Listing All Threads
public static void listThreads() { ThreadGroup root = Thread.currentThread() .getThreadGroup().getParent(); while (root.getParent() != null) { root = root.getParent(); } visitGroup(root, 0); } public static void visitGroup(ThreadGroup group, int level) { int numThreads = group.activeCount(); Thread[] threads = new Thread[numThreads]; group.enumerate(threads, false); for (int i=0; i<numThreads; i++) { Thread thread = threads[i]; printThreadInfo(thread); } int numGroups = group.activeGroupCount(); ThreadGroup[] groups = new ThreadGroup[numGroups]; numGroups = group.enumerate(groups, false); for (int i=0; i<numGroups; i++) { visitGroup(groups[i], level+1); } } private static void printThreadInfo(Thread t) { System.out.println("Thread: ... |
Get Java™ Phrasebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.