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 findThreadByName(String name) {
    ThreadGroup group = Thread.currentThread().getThreadGroup();

    while (group.getParent() != null)
        group = group.getParent();/*from w  ww  .  ja  v a2s  .com*/
    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

/** Collect info about a thread group */
private static void collectThreadGroupInfo(final ThreadGroup g, final StringBuffer str, final String indent) {
    if (g == null) {
        return;//from  w ww .j ava  2s  .  co  m
    }
    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

/**
 * Print diagnostic info about the current thread.
 *//*from w  ww.  j  av  a2 s . co  m*/
public static void printThreadInfo() {
    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();
    if (backtrace.length > 2) {
        System.out.printf("    trace[2]: %s\n", backtrace[2]);
    }
}

From source file:Main.java

public static String dumpThreads() {
    StringBuilder sb = new StringBuilder("\n");
    ThreadGroup tg, parent = Thread.currentThread().getThreadGroup();
    while ((tg = parent.getParent()) != null) {
        parent = tg;//from  w w w.j a v a 2s.co  m
    }
    int size = 2 * parent.activeGroupCount();
    ThreadGroup[] list = new ThreadGroup[size];
    int actual = parent.enumerate(list, true);
    //System.out.print("found " + list);
    sb.append("tgs: " + actual + "\n");
    if (list != null && list.length > 0) {
        for (ThreadGroup g : list) {
            if (g == null) {
                continue;
            }
            sb.append("   " + g.getName() + ": " + g.activeCount() + "\n");
            Thread[] threads = new Thread[g.activeCount()];
            g.enumerate(threads, false);
            for (Thread t : threads) {
                if (t != null) {
                    sb.append("     " + t.getName() + "\n");
                }
            }
        }
    } else {
        System.out.println("empty thread group list");
    }
    return sb.toString() + "\n";
}

From source file:Main.java

public static Thread getThreadByName(String name) {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();

    while (tg.getParent() != null) {

        tg = tg.getParent();//from ww  w .j  a  v a 2 s  .  c  o  m
    }

    Thread[] threads = new Thread[tg.activeCount() + 1024];

    tg.enumerate(threads, true);

    boolean bad_found = false;

    for (int i = 0; i < threads.length; i++) {

        Thread t = threads[i];

        if (t != null && t.isAlive() && t != Thread.currentThread() && !t.isDaemon()
                && t.getName().equals(name)) {
            return t;
        }
    }

    return null;
}

From source file:Main.java

public static Thread[] getThreads() {
    Thread currentThread = Thread.currentThread();

    ThreadGroup threadGroup = currentThread.getThreadGroup();

    while (threadGroup.getParent() != null) {
        threadGroup = threadGroup.getParent();
    }/*from w  w  w  . j av  a  2s  . c  o m*/

    int threadCountGuess = threadGroup.activeCount();

    Thread[] threads = new Thread[threadCountGuess];

    int threadCountActual = threadGroup.enumerate(threads);

    while (threadCountActual == threadCountGuess) {
        threadCountGuess *= 2;

        threads = new Thread[threadCountGuess];

        threadCountActual = threadGroup.enumerate(threads);
    }

    return threads;
}

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 w  w  w .  j  a  va 2 s.  c  om
 * @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//from   ww w .  jav a 2s . 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 java.util.Arrays.copyOf(threads, n);
}

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.
 * //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);
}

From source file:Main.java

/**
 * Get all threads/*from   w w  w .jav a  2s.c om*/
 * 
 * @return
 */
public static String[] getThreadNames() {
    ThreadGroup group = Thread.currentThread().getThreadGroup();
    ThreadGroup parent = null;
    while ((parent = group.getParent()) != null) {
        group = parent;
    }
    Thread[] threads = new Thread[group.activeCount()];
    group.enumerate(threads);
    HashSet<String> set = new HashSet<String>();

    for (int i = 0; i < threads.length; ++i) {
        if (threads[i] != null && threads[i].isAlive()) {
            try {
                set.add(threads[i].getThreadGroup().getName() + ", " + threads[i].getName() + ", "
                        + threads[i].getPriority() + ", " + threads[i].getState());
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
    String[] result = (String[]) set.toArray(new String[0]);
    Arrays.sort(result);

    for (int i = 0; i < result.length; i++) {
        //         logger.debug(result[i]);
    }

    return result;
}