Example usage for java.lang ThreadGroup activeGroupCount

List of usage examples for java.lang ThreadGroup activeGroupCount

Introduction

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

Prototype

public int activeGroupCount() 

Source Link

Document

Returns an estimate of the number of active groups in this thread group and its subgroups.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ThreadGroup group = Thread.currentThread().getThreadGroup().getParent();
    while (group.getParent() != null) {
        group = group.getParent();/* www . jav a2 s .c om*/
    }
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);

    for (int i = 0; i < numThreads; i++) {
        Thread thread = threads[i];
        System.out.println(thread.getName());
    }

    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);

    for (int i = 0; i < numGroups; i++) {
        System.out.println(groups[i]);
    }
}

From source file:Main.java

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

    int nAlloc = root.activeGroupCount();
    int n = 0;// ww  w  . ja  va 2s  .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:ThreadLister.java

/** Display info about a thread group */
private static void printGroupInfo(ThreadGroup g, String indent) {
    if (g == null)
        return;//from  w w  w.j  a  v a  2s . co  m
    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 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.
 * //ww w .ja  v a 2  s.com
 * @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: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 w ww .  jav  a  2s  .  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 thread group by name//from   ww w .  j  av  a 2s.com
 *
 * @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

/**
 * Display info about a thread group and its threads and groups
 *//*from w w  w  .j  ava 2  s .co  m*/
private static void printGroupInfo(PrintWriter out, ThreadGroup g, String indent) {
    if (g == null) {
        return;
    }
    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

/** Collect info about a thread group */
private static void collectThreadGroupInfo(final ThreadGroup g, final StringBuffer str, final String indent) {
    if (g == null) {
        return;//from w w w. j a va  2 s.c om
    }
    int numThreads = g.activeCount();
    int numGroups = g.activeGroupCount();
    final Thread[] threads = new Thread[numThreads * 2];
    final ThreadGroup[] groups = new ThreadGroup[numGroups * 2];

    numThreads = g.enumerate(threads, false);
    numGroups = g.enumerate(groups, false);

    str.append(indent).append("Thread Group: [").append(g.getName()).append("]  Max Priority: [")
            .append(g.getMaxPriority()).append(g.isDaemon() ? " Daemon" : "").append("]")
            .append(LINE_SEPARATOR);

    for (int i = 0; i < numThreads; i++) {
        if (threads[i] != null) {
            collectThreadInfo(threads[i], str, indent + "    ");
        }
    }
    for (int i = 0; i < numGroups; i++) {
        collectThreadGroupInfo(groups[i], str, indent + "    ");
    }
}

From source file:Main.java

public static void visit(ThreadGroup group, int level, StringBuffer buffer) {
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);
    for (int i = 0; i < numThreads; i++) {
        Thread t = threads[i];//from   w w  w .  j  a v  a  2 s.c  o  m
        for (int j = 0; j < level; j++) {
            buffer.append("  ");
        }
        buffer.append(t.toString()).append("\r");
    }

    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);
    for (int i = 0; i < numGroups; i++) {
        for (int j = 0; j < level; j++) {
            buffer.append("  ");
        }
        buffer.append(groups[i].toString()).append("\r");
        visit(groups[i], level + 1, buffer);
    }
}

From source file:Main.java

/**
 * This method recursively visits (log.info()) all thread groups under `group'.
 * //from w  w  w. java2 s.  c  om
 * @param log
 * @param logLevel
 */
public static void logThreadGroup(Logger log, Level logLevel, ThreadGroup group, int level) {
    // Get threads in `group'
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);

    // Enumerate each thread in `group'
    for (int i = 0; i < numThreads; i++) {
        // Get thread/
        Thread thread = threads[i];
        log.log(logLevel, thread.toString());
    }

    // 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++) {
        logThreadGroup(log, logLevel, groups[i], level + 1);
    }
}