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

/**
 * Display info about a thread group and its threads and groups
 *///from   ww w .  ja v a  2 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

/**
 * Gets all threads in the JVM.  This is really a snapshot of all threads
 * at the time this method is called.//w  w w  .  jav a  2 s  . co  m
 * @return An array of all threads currently running in the JVM.
 */
static public Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}

From source file:Main.java

public static Thread[] getAllThreads() {
    final ThreadGroup rootThreadGroup = getRootThreadGroup();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    int nAlloc = threadMXBean.getThreadCount();
    int n = 0;//from   www  .j  av a 2  s .c o m
    Thread[] threads;

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

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

From source file:Main.java

public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();

    int nAlloc = thbean.getThreadCount();
    int n = 0;/* w w w  . jav  a 2  s .c om*/

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

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

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

/** Returns a list of all threads.
 *
 * @return a list of all threads//from   w  ww . j  a va2  s.  c  o m
 */
public static List<Thread> getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    @SuppressWarnings("UnusedAssignment")
    int n = 0;
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    final List<Thread> threadList = new ArrayList<>();
    for (final Thread thread : threads) {
        if (thread != null) {
            threadList.add(thread);
        }
    }
    return threadList;
}

From source file:Main.java

/**
 * Get a list of all threads in a thread group.  An empty
 * array is returned if there are no threads in the group.
 *
 * @param   group   the thread group to list
 * @return      an array of threads/*from  ww w  .  j ava2  s . c o  m*/
 * @throws   NullPointerException
 *          if the group is null
 */
public static Thread[] getGroupThreads(final ThreadGroup group) {
    if (group == null)
        throw new NullPointerException("Null group");
    int nAlloc = group.activeCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = group.enumerate(threads, false);
    } while (n == nAlloc);
    return threads.clone();
}

From source file:Main.java

/**
 * Get a list of all threads in a thread group.  An empty
 * array is returned if there are no threads in the group.
 *
 * @param   group   the thread group to list
 * @return      an array of threads/*  w  w  w.j  a  v  a 2  s. com*/
 * @throws   NullPointerException
 *          if the group is null
 */
public static Thread[] getGroupThreads(final ThreadGroup group) {
    if (group == null)
        throw new NullPointerException("Null group");
    int nAlloc = group.activeCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = group.enumerate(threads, false);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}

From source file:Main.java

/**
 * Get a list of all threads.  Since there is always at
 * least one thread, this method never returns null or
 * an empty array.// w  ww .j ava2 s. c  om
 *
 * @return      an array of threads
 */
public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return threads.clone();
}

From source file:Main.java

/**
 * Get a list of all threads in a thread group. An empty array is returned
 * if there are no threads in the group.
 * //from  w  w  w. j  av a2s.c  o m
 * @param group
 *        the thread group to list
 * @return an array of threads
 * @throws NullPointerException
 *         if the group is null
 */
public static Thread[] getGroupThreads(final ThreadGroup group) {
    if (group == null) {
        throw new NullPointerException("Null group");
    }
    int nAlloc = group.activeCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = group.enumerate(threads, false);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}