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

public static Thread[] getThreadArray(ThreadGroup group) {
    Thread[] threads = new Thread[group.activeCount()];
    for (;;) {/*from w  w w  . j a  v  a 2s  . com*/
        int l = group.enumerate(threads);
        if (l == threads.length)
            break;
        threads = new Thread[l];
    }
    return threads;
}

From source file:Main.java

public static void dumpThreads() {

    ThreadGroup group = Thread.currentThread().getThreadGroup();
    int activeCount = group.activeCount();
    Thread[] threads = new Thread[activeCount];
    group.enumerate(threads);/*ww  w . j  a v a  2  s . c  om*/

    System.out.println("Thread-Group " + group + " contains " + activeCount + " threads");

    for (Thread thread : threads) {
        System.out.println("Thread " + thread);
    }
}

From source file:Main.java

public static List<Thread> getThreads(ThreadGroup tg, boolean recursive) {
    return getThreads(tg, recursive, tg.activeCount() + 10);
}

From source file:ThreadLister.java

/** Display info about a thread group */
private static void printGroupInfo(ThreadGroup g, String indent) {
    if (g == null)
        return;//www  .j  a v a 2 s  .c o  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

public static void threadGroupJoin(ThreadGroup threadGroup) throws InterruptedException {

    synchronized (threadGroup) {

        while (threadGroup.activeCount() > 0) {

            threadGroup.wait(10);//from w  w w .  j  av  a  2s.  co m
        }
    }
}

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];//w w  w. j a  v  a2  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

public static List<String> namesOfActiveThreadsIn(ThreadGroup grp) {
    List<String> threadNames = new ArrayList<String>();
    Thread[] threads = new Thread[grp.activeCount() * 10];
    int count = grp.enumerate(threads);
    for (int i = 0; i < count; i++) {
        threadNames.add(threads[i].getName());
    }/*from w ww .jav a  2s  .  c  o  m*/
    return threadNames;
}

From source file:Main.java

/**
 * Get all the threads in this JVM./* ww  w .  j  a va2 s .c o  m*/
 *
 * @return all the threads
 */
public static final List<Thread> getAllThreads() {
    ThreadGroup topGroup = topGroup();
    return getAllThreadsInGroup(topGroup, topGroup.activeCount());
}

From source file:Main.java

/**
 * This method recursively visits all thread groups under group.
 * //from   w ww.j  a v  a2  s  .  c  o  m
 * @param group
 *            {@link ThreadGroup}
 * @param level
 *            a thread level
 */
public static void viewRunningThreads(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];
        System.out.println(thread.getState() + ", " + thread.getName());
    }

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

From source file:Mem.java

public static void dumpThreadInfo() {
    ThreadGroup g = Thread.currentThread().getThreadGroup();
    Thread[] t = new Thread[g.activeCount()];
    g.enumerate(t);//from  w w  w .  j  av  a  2  s.c o  m
    System.err.println("Active threads in " + g);
    for (int i = 0; i < t.length; ++i)
        System.err.println(t[i]);
}