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 void main(String[] args) throws Exception {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();

    MyThread mt1 = new MyThread(tg, "first");
    MyThread mt2 = new MyThread(tg, "second");

    mt1.start();/*  w  w  w  .j  ava2  s  . com*/
    mt2.start();

    ThreadGroup parent = tg.getParent();
    Thread[] list = new Thread[parent.activeCount()];
    int count = parent.enumerate(list, true);
    String[] thdinfo = new String[count];
    for (int i = 0; i < count; i++)
        thdinfo[i] = list[i].toString();

    mt1.join();
    mt2.join();

    for (int i = 0; i < count; i++)
        System.out.println(thdinfo[i]);
}

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();// ww w  . j  ava2  s  .  c o m
    }
    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 List<Thread> getThreads(ThreadGroup tg, boolean recursive, int estimate) {
    Thread[] threads = new Thread[estimate];
    int count = tg.enumerate(threads, recursive);
    if (count == estimate)
        return getThreads(tg, recursive, estimate + 10);
    else/* ww  w . j  a va  2 s .c o m*/
        return Arrays.asList(threads).subList(0, count);
}

From source file:Main.java

/**
 * Get all the threads in this thread group.
 *
 * @param group the specified thread group
 * @param size the group size//from www.  ja va2s.c  om
 * @return all the threads in the specified thread group
 */
public static final List<Thread> getAllThreadsInGroup(ThreadGroup group, int size) {
    Thread[] array = null;
    do {
        array = new Thread[size + 100];
        size = group.enumerate(array, true);
    } while (size >= array.length);
    List<Thread> ret = new ArrayList<Thread>(size);
    for (int i = 0; i < size; ++i)
        ret.add(array[i]);
    return ret;
}

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  ww  w. j  av a  2  s.  com
        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 Thread findThreadByName(String name) {
    ThreadGroup group = Thread.currentThread().getThreadGroup();

    while (group.getParent() != null)
        group = group.getParent();/*from w ww .  ja  v  a  2 s .co m*/
    Thread[] threadList = new Thread[group.activeCount() + 5];
    group.enumerate(threadList, true);
    for (Thread thread : threadList)
        if (thread != null && thread.getName().equals(name))
            return thread;
    return null;
}

From source file:Main.java

/**
 * This method recursively visits all thread groups under group.
 * //from ww w .  jav a 2s  .  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:Main.java

/**
 * This method recursively visits (log.info()) all thread groups under `group'.
 * /*from w w  w  .  ja  va 2 s . com*/
 * @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:Main.java

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

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]);//w  ww.ja va  2s .  co 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);
    }
}