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

public static Thread getThreadByName(String name) {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();

    while (tg.getParent() != null) {

        tg = tg.getParent();/* ww w. ja  va  2s . co  m*/
    }

    Thread[] threads = new Thread[tg.activeCount() + 1024];

    tg.enumerate(threads, true);

    boolean bad_found = false;

    for (int i = 0; i < threads.length; i++) {

        Thread t = threads[i];

        if (t != null && t.isAlive() && t != Thread.currentThread() && !t.isDaemon()
                && t.getName().equals(name)) {
            return t;
        }
    }

    return null;
}

From source file:Main.java

public static void visit(ThreadGroup group, int level, List<Thread> result) {
    // Get threads in `group'
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[Math.max(numThreads, 2) * 2];
    numThreads = group.enumerate(threads, false);

    // Enumerate each thread in `group'
    for (int i = 0; i < numThreads; i++) {
        // Get thread
        Thread thread = threads[i];
        result.add(thread);/*  ww  w  .j a v a 2 s .c o m*/
    }

    // 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, result);
    }

}

From source file:ThreadLister.java

/** Display info about a thread group */
private static void printGroupInfo(ThreadGroup g, String indent) {
    if (g == null)
        return;/*ww w  .j av a 2 s.  c om*/
    int numThreads = g.activeCount();
    int numGroups = g.activeGroupCount();
    Thread[] threads = new Thread[numThreads];
    ThreadGroup[] groups = new ThreadGroup[numGroups];

    g.enumerate(threads, false);
    g.enumerate(groups, false);

    System.out.println(indent + "Thread Group: " + g.getName() + "  Max Priority: " + g.getMaxPriority()
            + (g.isDaemon() ? " Daemon" : ""));

    for (int i = 0; i < numThreads; i++)
        printThreadInfo(threads[i], indent + "    ");
    for (int i = 0; i < numGroups; i++)
        printGroupInfo(groups[i], indent + "    ");
}

From source file:Main.java

/**
 * Get thread group by name//from   w  w  w.jav a2  s  .  c  om
 *
 * @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;
}

From source file:Main.java

public static ThreadGroup[] getAllThreadGroups() {
    final ThreadGroup root = getRootThreadGroup();

    int nAlloc = root.activeGroupCount();
    int n = 0;//from  w  w w.  j  a v a 2 s  . c  o m
    ThreadGroup[] groups;

    do {
        nAlloc *= 2;
        groups = new ThreadGroup[nAlloc];
        n = root.enumerate(groups, true);
    } while (n == nAlloc);

    ThreadGroup[] allGroups = new ThreadGroup[n + 1];
    allGroups[0] = root;
    System.arraycopy(groups, 0, allGroups, 1, n);

    return allGroups;
}

From source file:Main.java

public static String dumpThreads() {
    StringBuilder sb = new StringBuilder("\n");
    ThreadGroup tg, parent = Thread.currentThread().getThreadGroup();
    while ((tg = parent.getParent()) != null) {
        parent = tg;/* ww  w  .  j a v a2  s  .co  m*/
    }
    int size = 2 * parent.activeGroupCount();
    ThreadGroup[] list = new ThreadGroup[size];
    int actual = parent.enumerate(list, true);
    //System.out.print("found " + list);
    sb.append("tgs: " + actual + "\n");
    if (list != null && list.length > 0) {
        for (ThreadGroup g : list) {
            if (g == null) {
                continue;
            }
            sb.append("   " + g.getName() + ": " + g.activeCount() + "\n");
            Thread[] threads = new Thread[g.activeCount()];
            g.enumerate(threads, false);
            for (Thread t : threads) {
                if (t != null) {
                    sb.append("     " + t.getName() + "\n");
                }
            }
        }
    } else {
        System.out.println("empty thread group list");
    }
    return sb.toString() + "\n";
}

From source file:ThreadLister.java

/** Display info about a thread group and its threads and groups */
private static void printGroupInfo(PrintWriter out, ThreadGroup g, String indent) {
    if (g == null)
        return;/*from ww  w  .j a v  a2  s  . c  om*/
    int num_threads = g.activeCount();
    int num_groups = g.activeGroupCount();
    Thread[] threads = new Thread[num_threads];
    ThreadGroup[] groups = new ThreadGroup[num_groups];

    g.enumerate(threads, false);
    g.enumerate(groups, false);

    out.println(indent + "Thread Group: " + g.getName() + "  Max Priority: " + g.getMaxPriority()
            + (g.isDaemon() ? " Daemon" : ""));

    for (int i = 0; i < num_threads; i++)
        printThreadInfo(out, threads[i], indent + "    ");
    for (int i = 0; i < num_groups; i++)
        printGroupInfo(out, groups[i], indent + "    ");
}

From source file:Main.java

/**
 * Get a list of all thread groups. Since there is always at least one
 * thread group (the root), this method never returns a null or empty array.
 * /*from   ww w  .j ava 2 s .  c  om*/
 * @return an array of thread groups
 */
public static ThreadGroup[] getAllThreadGroups() {
    final ThreadGroup root = getRootThreadGroup();
    int nAlloc = root.activeGroupCount();
    int n = 0;
    ThreadGroup[] groups = null;
    do {
        nAlloc *= 2;
        groups = new ThreadGroup[nAlloc];
        n = root.enumerate(groups, true);
    } while (n == nAlloc);
    ThreadGroup[] allGroups = new ThreadGroup[n + 1];
    allGroups[0] = root;
    System.arraycopy(groups, 0, allGroups, 1, n);
    return allGroups;
}

From source file:Main.java

public static Thread[] getAllThreads() {

    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();

    int nAlloc = thbean.getThreadCount();
    int n = 0;/*from www.j  a va  2s.  co  m*/
    Thread[] threads;

    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:Main.java

public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;/* w  ww. j a v  a 2  s  . c  o  m*/
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}