Example usage for java.lang ThreadGroup enumerate

List of usage examples for java.lang ThreadGroup enumerate

Introduction

In this page you can find the example usage for java.lang ThreadGroup enumerate.

Prototype

public int enumerate(ThreadGroup list[], boolean recurse) 

Source Link

Document

Copies into the specified array references to every active subgroup in this thread group.

Usage

From source file:Main.java

/**
 * Get all threads./*  w ww .  jav a  2  s .com*/
 */
public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    int alloc = threadMXBean.getThreadCount();
    int size = 0;
    Thread[] threads;
    // ThreadGroup.enumerate() will only return as many threads as it can fit in
    // the array it's given, and we have no accurate way to know how many threads
    // there will be at the time it is called.
    do {
        alloc *= 2;
        threads = new Thread[alloc];
        size = root.enumerate(threads, true);
    } while (size >= alloc);
    Thread[] trimmed = new Thread[size];
    System.arraycopy(threads, 0, trimmed, 0, size);
    return trimmed;
}

From source file:Main.java

/**
 * Get a list of all threads. Since there is always at least one thread,
 * this method never returns null or an empty array.
 * /* w  w  w . j  av  a  2  s. com*/
 * @return an array of threads
 */
public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}

From source file:org.objectweb.proactive.extensions.timitspmd.TimIt.java

@SuppressWarnings("deprecation")
public static void threadsCleaning() {
    ThreadGroup tg = Thread.currentThread().getThreadGroup().getParent();
    Thread[] threads = new Thread[200];
    int len = tg.enumerate(threads, true);
    int nbKilled = 0;

    for (int i = 0; i < len; i++) {
        Thread ct = threads[i];/* w w w. j a  v a  2 s  .c  o  m*/

        if ((ct.getName().indexOf("RMI RenewClean") >= 0) || (ct.getName().indexOf("ThreadInThePool") >= 0)) {
            nbKilled++;
            ct.stop();
        }
    }

    System.err.println(nbKilled + " thread(s) stopped on " + len);
}

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 ww.j ava 2s  .co  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.google.gdt.eclipse.designer.hosted.tdt.HostedModeSupport.java

/**
 * @return array of {@link Thread}s, may be with <code>null</code> on the end.
 *//*from ww  w.java2  s . co  m*/
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;
}

From source file:net.bull.javamelody.internal.model.JavaInformations.java

static List<Thread> getThreadsFromThreadGroups() {
    ThreadGroup group = Thread.currentThread().getThreadGroup(); // NOPMD
    while (group.getParent() != null) {
        group = group.getParent();/*w w w  .  j  ava 2  s . c  om*/
    }
    final Thread[] threadsArray = new Thread[group.activeCount()];
    group.enumerate(threadsArray, true);
    return Arrays.asList(threadsArray);
}

From source file:net.wastl.webmail.misc.Helper.java

static public void logThreads(String label) {
    ThreadGroup tG = Thread.currentThread().getThreadGroup();
    // These pointers are cheap.
    // Since these counts probably don't count nested items, we have
    // to allocate liberally.
    Thread[] threads = new Thread[tG.activeCount() * 10];
    ThreadGroup[] threadGroups = new ThreadGroup[tG.activeGroupCount() * 10];
    int tCount = tG.enumerate(threads, true);
    int tgCount = tG.enumerate(threadGroups, true);
    threadDumperLog.debug(label + ".  Recursive counts.  ThreadGroups: " + tgCount + ", Threads: " + tCount);
    for (int i = 0; i < tCount; i++)
        threadDumperLog.debug(threads[i].toString());
    for (int i = 0; i < tgCount; i++)
        threadDumperLog.debug(threadGroups[i].toString());
}

From source file:org.openmrs.util.OpenmrsClassLoader.java

private static List<Thread> listThreads(ThreadGroup group, String indent) {
    List<Thread> threadToReturn = new ArrayList<Thread>();

    log.error(indent + "Group[" + group.getName() + ":" + group.getClass() + "]");
    int nt = group.activeCount();
    Thread[] threads = new Thread[nt * 2 + 10]; //nt is not accurate
    nt = group.enumerate(threads, false);

    // List every thread in the group
    for (int i = 0; i < nt; i++) {
        Thread t = threads[i];//from  ww w. j av  a2s  .co m
        log.error(indent + "  Thread[" + t.getName() + ":" + t.getClass() + ":"
                + (t.getContextClassLoader() == null ? "null cl"
                        : t.getContextClassLoader().getClass().getName() + " "
                                + t.getContextClassLoader().hashCode())
                + "]");
        threadToReturn.add(t);
    }

    // Recursively list all subgroups
    int ng = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[ng * 2 + 10];
    ng = group.enumerate(groups, false);

    for (int i = 0; i < ng; i++) {
        threadToReturn.addAll(listThreads(groups[i], indent + "  "));
    }

    return threadToReturn;
}

From source file:com.dianping.dpsf.jmx.DpsfRequestorMonitor.java

private int getThreadCount(State state) {
    ThreadGroup threadGroup = clientManager.getClientResponseThreadPool().getFactory().getGroup();
    Thread[] threads = new Thread[threadGroup.activeCount()];
    threadGroup.enumerate(threads, false);
    int threadCount = 0;
    for (Thread t : threads) {
        if (state == t.getState()) {
            threadCount++;/*from  w w  w.  j a  va 2  s . com*/
        }
    }
    return threadCount;
}

From source file:Main.java

public ThreadGroupDemo() {
    ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");

    ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

    Thread t1 = new Thread(pGroup, this);
    System.out.println("Starting " + t1.getName());
    t1.start();/*from   w w w  . j a va 2 s  .c  o  m*/

    Thread t2 = new Thread(cGroup, this);
    System.out.println("Starting " + t2.getName());
    t2.start();

    Thread[] list = new Thread[pGroup.activeCount()];
    int count = pGroup.enumerate(list, true);
    for (int i = 0; i < count; i++) {
        System.out.println("Thread " + list[i].getName() + " found");
    }

}