Example usage for java.lang ThreadGroup activeCount

List of usage examples for java.lang ThreadGroup activeCount

Introduction

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

Prototype

public int activeCount() 

Source Link

Document

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

Usage

From source file:Main.java

/**
 * This method recursively visits (LOG.info()) all thread groups under
 * `group'.//  w  w  w.j a  v a 2s .c  om
 * 
 * @param log the {@link Logger}to be used for logging
 */
public static void logThreadGroup(final Logger log, ThreadGroup group, int level) {
    // Get threads in `group'
    int numThreads = group.activeCount();
    final 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/
        final Thread thread = threads[i];
        log.info(thread.toString());
    }

    // Get thread subgroups of `group'
    int numGroups = group.activeGroupCount();
    final ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);

    // Recursively visit each subgroup
    for (int i = 0; i < numGroups; i++) {
        logThreadGroup(log, groups[i], level + 1);
    }
}

From source file:Main.java

/**
 * This method recursively visits (log.info()) all thread groups under `group'.
 * /*  w ww.j  a va2 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);
    }
}

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  w  w.  ja v  a  2  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

public static Thread[] getAllThreadsInGroup(ThreadGroup group) {
    if (group != null) {
        int nAlloc = group.activeCount();
        int n = 0;
        Thread[] threads;/*from  ww w . j a v  a 2 s  .c o m*/

        do {
            nAlloc *= 2;
            threads = new Thread[nAlloc];
            n = group.enumerate(threads);
        } while (n == nAlloc);

        return java.util.Arrays.copyOf(threads, n);
    }

    return new Thread[0];
}

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);/*w ww  .j a va2s  .  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:Main.java

public static List<Thread> getGroupThreads(ThreadGroup threadGroup) {
    if (threadGroup == null) {
        return Collections.emptyList();
    }//from w ww .  j a  v  a  2 s.co m

    Thread[] threadArray = new Thread[threadGroup.activeCount() * 2];
    threadGroup.enumerate(threadArray);
    return Arrays.asList(threadArray);
}

From source file:Main.java

private static void visit(List<Thread> list, ThreadGroup group, int level, boolean includeDaemons) {

    // 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
        if (!includeDaemons && threads[i].isDaemon())
            continue;

        list.add(threads[i]);//  ww w.  jav  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(list, groups[i], level + 1, includeDaemons);
    }
}

From source file:Main.java

/**
 * Display info about a thread group and its threads and groups
 *//*from ww w .j a va2 s. c  o 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

/**
 * @param name/*from  w w w.j a v a 2 s.  c  o  m*/
 * @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;
}

From source file:Main.java

/**
 * Print a stack trace of the current thread.
 *//*from   w ww .j  a va2  s  .  c om*/
public static void printFullStackTrace() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();

    for (StackTraceElement e : backtrace) {
        System.out.printf("    Stack Trace: %s\n", e);
    }
}