List of usage examples for java.lang ThreadGroup activeCount
public int activeCount()
From source file:ThreadViewer.java
public static Thread[] findAllThreads() { ThreadGroup group = Thread.currentThread().getThreadGroup(); ThreadGroup topGroup = group; while (group != null) { topGroup = group;// ww w.j a va 2 s.c om group = group.getParent(); } int estimatedSize = topGroup.activeCount() * 2; Thread[] slackList = new Thread[estimatedSize]; int actualSize = topGroup.enumerate(slackList); Thread[] list = new Thread[actualSize]; System.arraycopy(slackList, 0, list, 0, actualSize); return list; }
From source file:org.nuxeo.runtime.management.jvm.ThreadDeadlocksDetector.java
protected static Map<Long, Thread> getThreads() { ThreadGroup root = rootGroup(Thread.currentThread().getThreadGroup()); int nThreads = root.activeCount(); Thread[] threads = new Thread[2 * nThreads]; root.enumerate(threads);//from w w w. jav a 2s . c om Map<Long, Thread> map = new HashMap<Long, Thread>(threads.length); for (Thread thread : threads) { if (thread == null) { continue; } map.put(thread.getId(), thread); } return map; }
From source file:org.eclipse.objectteams.otequinox.internal.hook.Util.java
private static int getActiveCount() { ThreadGroup group = Thread.currentThread().getThreadGroup(); ThreadGroup parent = group.getParent(); while (parent != null) { group = parent;// www . j a va 2 s. c o m parent = group.getParent(); } return group.activeCount(); }
From source file:es.darkhogg.hazelnutt.Hazelnutt.java
/** * Terminates the application in at most <i>time</i> milliseconds for * every alive thread. /*from w w w. j a v a 2 s .co m*/ * * @param time Number of milliseconds to wait for each thread to terminate */ public static void terminate(long time) { Logger logger = getLogger(); logger.info("Terminating application..."); try { getFrame().dispose(); // Get the root thread group ThreadGroup rootThreadGroup = Thread.currentThread().getThreadGroup(); while (rootThreadGroup.getParent() != null) { rootThreadGroup = rootThreadGroup.getParent(); } // Declare some collections Queue<ThreadGroup> threadGroups = new LinkedList<ThreadGroup>(); Queue<Thread> threads = new LinkedList<Thread>(); // Get ALL groups threadGroups.add(rootThreadGroup); while (!threadGroups.isEmpty()) { ThreadGroup group = threadGroups.remove(); Thread[] subThreads = new Thread[group.activeCount() * 2]; //group.enumerate( subThreads ); for (Thread subThread : subThreads) { if (subThread != null) { threads.add(subThread); } } ThreadGroup[] subThreadGroups = new ThreadGroup[group.activeGroupCount() * 2]; for (ThreadGroup subThreadGroup : subThreadGroups) { if (subThreadGroup != null) { threadGroups.add(subThreadGroup); } } } // Join a maximum of time milliseconds for all non-daemon threads while (!threads.isEmpty()) { Thread thread = threads.remove(); LOGGER.trace(thread); if (!thread.isDaemon() && thread != Thread.currentThread()) { logger.trace("Waiting for thread '" + thread.getName() + "'"); thread.join(time); if (thread.isAlive()) { logger.trace("Interrupting thread '" + thread.getName() + "'"); thread.interrupt(); } } } } catch (Throwable e) { LOGGER.warn("Interrupted while terminating application", e); } finally { // Exit the program System.exit(0); } }
From source file:Main.java
/** * Returns a list of all the threads in the current context. * * @return A list of all the threads in the current context. *//* w w w .j a v a 2s. c o m*/ public static Thread[] listThreads() { ThreadGroup root = getRootThreadGroup(); int threadCount = 0, iteration = 0; Thread[] list = new Thread[threadCount]; // because ThreadGroup.enumerate isn't thread save, keep trying to // enumerate for up to 10 times until we happen to have an array // large enough to hold all the threads that exist at the moment // enumerate is called while (iteration < 10 && threadCount >= list.length) { list = new Thread[root.activeCount() + (500 * ++iteration)]; threadCount = root.enumerate(list, true); } return Arrays.copyOf(list, threadCount); }
From source file:com.googlecode.psiprobe.Utils.java
public static Thread getThreadByName(String name) { if (name != null) { ///* w w w.ja v a 2 s. c o m*/ // get top ThreadGroup // ThreadGroup masterGroup = Thread.currentThread().getThreadGroup(); while (masterGroup.getParent() != null) { masterGroup = masterGroup.getParent(); } Thread[] threads = new Thread[masterGroup.activeCount()]; int numThreads = masterGroup.enumerate(threads); for (int i = 0; i < numThreads; i++) { if (threads[i] != null && name.equals(threads[i].getName())) { return threads[i]; } } } return null; }
From source file:org.springframework.data.hadoop.mapreduce.ExecutionUtils.java
/** * Returns the threads running inside the current JVM. * /* w w w. ja va 2 s. co m*/ * @return running threads */ static Thread[] threads() { // Could have used the code below but it tends to be somewhat ineffective and slow // Set<Thread> threadSet = Thread.getAllStackTraces().keySet(); // Get the current thread group ThreadGroup tg = Thread.currentThread().getThreadGroup(); // Find the root thread group while (tg.getParent() != null) { tg = tg.getParent(); } int threadCountGuess = tg.activeCount() + 50; Thread[] threads = new Thread[threadCountGuess]; int threadCountActual = tg.enumerate(threads); // Make sure we don't miss any threads while (threadCountActual == threadCountGuess) { threadCountGuess *= 2; threads = new Thread[threadCountGuess]; // Note tg.enumerate(Thread[]) silently ignores any threads that // can't fit into the array threadCountActual = tg.enumerate(threads); } return threads; }
From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java
private static Thread[] getThreads() { // Get the current thread group ThreadGroup tg = Thread.currentThread().getThreadGroup(); // Find the root thread group while (tg.getParent() != null) { tg = tg.getParent();/* ww w .j a v a2s .c o m*/ } int threadCountGuess = tg.activeCount() + 50; Thread[] threads = new Thread[threadCountGuess]; int threadCountActual = tg.enumerate(threads); // Make sure we don't miss any threads while (threadCountActual == threadCountGuess) { threadCountGuess *= 2; threads = new Thread[threadCountGuess]; // Note tg.enumerate(Thread[]) silently ignores any threads that // can't fit into the array threadCountActual = tg.enumerate(threads); } return threads; }
From source file:psiprobe.Utils.java
/** * Gets the thread by name.//from w ww . ja v a 2 s . co m * * @param name the name * @return the thread by name */ public static Thread getThreadByName(String name) { if (name != null) { // get top ThreadGroup ThreadGroup masterGroup = Thread.currentThread().getThreadGroup(); while (masterGroup.getParent() != null) { masterGroup = masterGroup.getParent(); } Thread[] threads = new Thread[masterGroup.activeCount()]; int numThreads = masterGroup.enumerate(threads); for (int i = 0; i < numThreads; i++) { if (threads[i] != null && name.equals(threads[i].getName())) { return threads[i]; } } } return null; }
From source file:com.google.gdt.eclipse.designer.hosted.tdt.HostedModeSupport.java
/** * @return array of {@link Thread}s, may be with <code>null</code> on the end. *//*from w ww . ja v a 2s .c om*/ private static Thread[] getAllThreads() { // prepare root ThreadGroup ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); ThreadGroup parentGroup; while ((parentGroup = rootGroup.getParent()) != null) { rootGroup = parentGroup; } // fill Thread array Thread[] threads = new Thread[rootGroup.activeCount()]; while (rootGroup.enumerate(threads, true) == threads.length) { threads = new Thread[threads.length * 2]; } return threads; }