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

/**
 * This method recursively visits (LOG.info()) all thread groups under
 * `group'./*from w w  w. j  a  v  a  2s  . co  m*/
 * 
 * @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

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

/**
 * This method recursively visits all thread groups under group.
 * //from w  ww  .j ava2  s  . co 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: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]);/*from www .java 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:es.darkhogg.hazelnutt.Hazelnutt.java

/**
 * Terminates the application in at most <i>time</i> milliseconds for
 * every alive thread. /*w ww  .  j a  va2  s.  c o m*/
 * 
 * @param time Number of milliseconds to wait for each thread to terminate
 */
public static void terminate(long time) {
    Logger logger = getLogger();
    logger.info("Terminating application...");

    try {
        getFrame().dispose();

        // Get the root thread group
        ThreadGroup rootThreadGroup = Thread.currentThread().getThreadGroup();
        while (rootThreadGroup.getParent() != null) {
            rootThreadGroup = rootThreadGroup.getParent();
        }

        // Declare some collections
        Queue<ThreadGroup> threadGroups = new LinkedList<ThreadGroup>();
        Queue<Thread> threads = new LinkedList<Thread>();

        // Get ALL groups
        threadGroups.add(rootThreadGroup);
        while (!threadGroups.isEmpty()) {
            ThreadGroup group = threadGroups.remove();

            Thread[] subThreads = new Thread[group.activeCount() * 2];
            //group.enumerate( subThreads );
            for (Thread subThread : subThreads) {
                if (subThread != null) {
                    threads.add(subThread);
                }
            }

            ThreadGroup[] subThreadGroups = new ThreadGroup[group.activeGroupCount() * 2];
            for (ThreadGroup subThreadGroup : subThreadGroups) {
                if (subThreadGroup != null) {
                    threadGroups.add(subThreadGroup);
                }
            }
        }

        // Join a maximum of time milliseconds for all non-daemon threads
        while (!threads.isEmpty()) {
            Thread thread = threads.remove();
            LOGGER.trace(thread);

            if (!thread.isDaemon() && thread != Thread.currentThread()) {
                logger.trace("Waiting for thread '" + thread.getName() + "'");
                thread.join(time);
                if (thread.isAlive()) {
                    logger.trace("Interrupting thread '" + thread.getName() + "'");
                    thread.interrupt();
                }
            }
        }

    } catch (Throwable e) {
        LOGGER.warn("Interrupted while terminating application", e);

    } finally {
        // Exit the program
        System.exit(0);
    }
}

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: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  ww  w  . java 2  s.c o  m

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

    ThreadGroup[] grpList = new ThreadGroup[pGroup.activeGroupCount()];
    int count = pGroup.enumerate(grpList);
    for (int i = 0; i < count; i++) {
        System.out.println("ThreadGroup " + grpList[i].getName() + " found");
    }
}

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  ww w . j a  v  a  2 s. c  o  m*/

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

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

From source file:haven.Utils.java

private static void dumptg(ThreadGroup tg, PrintWriter out, int indent) {
    for (int o = 0; o < indent; o++)
        out.print("    ");
    out.println("G: \"" + tg.getName() + "\"");
    Thread[] ths = new Thread[tg.activeCount() * 2];
    ThreadGroup[] tgs = new ThreadGroup[tg.activeGroupCount() * 2];
    int nt = tg.enumerate(ths, false);
    int ng = tg.enumerate(tgs, false);
    for (int i = 0; i < nt; i++) {
        Thread ct = ths[i];/*from w w w  .j a v  a2s .  com*/
        for (int o = 0; o < indent + 1; o++)
            out.print("    ");
        out.println("T: \"" + ct.getName() + "\"");
    }
    for (int i = 0; i < ng; i++) {
        ThreadGroup cg = tgs[i];
        dumptg(cg, out, indent + 1);
    }
}

From source file:org.kaaproject.kaa.server.common.thrift.cli.server.BaseCliThriftService.java

/**
 * Retrieve all threads info from thread group.
 *
 * @param group      the thread group// w w w . j a  v  a  2s .  c  o m
 * @param threadsMap the threads map to store threads info
 */
private void allThreadsFromGroup(ThreadGroup group, Map<Long, ThreadStruct> threadsMap) {
    int threadCount = group.activeCount();
    int groupCount = group.activeGroupCount();
    Thread[] groupThreads = new Thread[threadCount];
    ThreadGroup[] threadGroups = new ThreadGroup[groupCount];
    group.enumerate(groupThreads, false);
    group.enumerate(threadGroups, false);
    for (Thread t : groupThreads) {
        if (t != null) {
            ThreadStruct ts = threadsMap.get(t.getId());
            ts.thread = t;
        }
    }
    for (ThreadGroup tg : threadGroups) {
        allThreadsFromGroup(tg, threadsMap);
    }
}