Java examples for java.lang:Thread
list Thread Group
//package com.java2s; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { String name = "java2s.com"; System.out.println(listThreadGroup(name)); }// w w w.j ava 2 s . c o m /** * @param name * @return list of threads */ public static List<Thread> listThreadGroup(String name) { List<Thread> threads = new LinkedList<Thread>(); ThreadGroup threadGroup = getThreadGroupByName(name, null); if (threadGroup != null) { int count = threadGroup.activeCount(); Thread threadArray[] = new Thread[count]; int threadCount = Thread.enumerate(threadArray); for (int index = 0; index < threadCount; index++) { threads.add(threadArray[index]); } } return threads; } /** * Get thread group by name * * @param name * @param root * @return thread group or null */ private static ThreadGroup getThreadGroupByName(String name, ThreadGroup root) { if (root == null) { root = getRootThreadGroup(); } int num_groups = root.activeGroupCount(); ThreadGroup[] groups = new ThreadGroup[num_groups]; root.enumerate(groups, false); for (int i = 0; i < num_groups; i++) { ThreadGroup threadGroup = groups[i]; if (name.equals(threadGroup.getName())) { return threadGroup; } else { threadGroup = getThreadGroupByName(name, threadGroup); if (threadGroup != null) { return threadGroup; } } } return null; } /** * Get root thread group * * @return root thread group */ private static ThreadGroup getRootThreadGroup() { ThreadGroup root = Thread.currentThread().getThreadGroup(); ThreadGroup parent = root.getParent(); while (parent != null) { root = parent; parent = parent.getParent(); } return root; } }