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: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:net.bull.javamelody.internal.model.JavaInformations.java

static List<Thread> getThreadsFromThreadGroups() {
    ThreadGroup group = Thread.currentThread().getThreadGroup(); // NOPMD
    while (group.getParent() != null) {
        group = group.getParent();/*from   w w w . ja v  a 2s. c o  m*/
    }
    final Thread[] threadsArray = new Thread[group.activeCount()];
    group.enumerate(threadsArray, true);
    return Arrays.asList(threadsArray);
}

From source file:org.psikeds.common.threadlocal.ThreadLocalHelper.java

/**
 * Get all active Threads of our Root-ThreadGroup.
 * //from   w  ww  .  java2s.  c om
 * @return Array of Threads
 */
private static Thread[] getThreads() {
    int threadCountActual = 0;
    try {
        LOGGER.trace("--> getThreads()");
        ThreadGroup rootgroup = Thread.currentThread().getThreadGroup();
        while (rootgroup.getParent() != null) {
            rootgroup = rootgroup.getParent();
        }
        // Note: ThreadGroup.enumerate(Thread[]) silently ignores any Thread
        // that won't fit into the Array. Therefore we must make sure that
        // we do not miss any Threads by continuously increasing the Size of
        // the Array.
        Thread[] threads = null;
        int threadCountGuess = rootgroup.activeCount();
        do {
            threadCountGuess *= 2;
            threads = new Thread[threadCountGuess];
            threadCountActual = rootgroup.enumerate(threads);
        } while (threadCountActual == threadCountGuess);
        return threads;
    } finally {
        LOGGER.trace("<-- getThreads(); #Threads = {}", threadCountActual);
    }
}

From source file:EnumerateMain.java

public void listCurrentThreads() {
    ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
    int numThreads = currentGroup.activeCount();
    Thread[] listOfThreads = new Thread[numThreads];

    currentGroup.enumerate(listOfThreads);
    for (int i = 0; i < numThreads; i++)
        System.out.println("Thread #" + i + " = " + listOfThreads[i].getName());
}

From source file:org.openmrs.util.OpenmrsClassLoader.java

private static List<Thread> listThreads(ThreadGroup group, String indent) {
    List<Thread> threadToReturn = new ArrayList<Thread>();

    log.error(indent + "Group[" + group.getName() + ":" + group.getClass() + "]");
    int nt = group.activeCount();
    Thread[] threads = new Thread[nt * 2 + 10]; //nt is not accurate
    nt = group.enumerate(threads, false);

    // List every thread in the group
    for (int i = 0; i < nt; i++) {
        Thread t = threads[i];//from  w  w  w.  j  a  v a2  s .  c o  m
        log.error(indent + "  Thread[" + t.getName() + ":" + t.getClass() + ":"
                + (t.getContextClassLoader() == null ? "null cl"
                        : t.getContextClassLoader().getClass().getName() + " "
                                + t.getContextClassLoader().hashCode())
                + "]");
        threadToReturn.add(t);
    }

    // Recursively list all subgroups
    int ng = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[ng * 2 + 10];
    ng = group.enumerate(groups, false);

    for (int i = 0; i < ng; i++) {
        threadToReturn.addAll(listThreads(groups[i], indent + "  "));
    }

    return threadToReturn;
}

From source file:net.sourceforge.vulcan.jabber.SmackKeepAliveThreadInterrupter.java

public void interrupt() {
    final ThreadGroup group = Thread.currentThread().getThreadGroup();

    final Thread[] threads = new Thread[group.activeCount()];

    group.enumerate(threads);// ww w  .  j  a  v a  2s  . c  om

    for (Thread thread : threads) {
        if (!thread.getName().startsWith("Smack Keep Alive")) {
            continue;
        }

        if (!thread.getContextClassLoader().equals(getClass().getClassLoader())) {
            // only wake up threads from our own class loader
            LOG.info("Not waking up " + thread.getName() + " because it uses a different class loader.");
            continue;
        }

        LOG.info("Interrupting " + thread.getName());

        thread.interrupt();

        try {
            thread.join(1000);
        } catch (InterruptedException ignore) {
        }

        if (thread.isAlive()) {
            LOG.error("Smack Keep Alive thread still alive after interruption.");
        }
    }
}

From source file:org.dacapo.harness.Tomcat.java

@SuppressWarnings("unused")
private void dumpThreads() {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    int nThreads = tg.activeCount();
    Thread[] threads = new Thread[nThreads * 2];
    nThreads = Thread.enumerate(threads);
    System.out.printf("==================== Dumping %d Threads: ====================%n", nThreads);
    System.out.flush();/*from w w  w  .j  a v  a 2 s  . c  o m*/
    for (int i = 0; i < nThreads; i++) {
        if (threads[i] != null) {
            System.out.print(threads[i].getName() + ": ");
            StackTraceElement[] stack = threads[i].getStackTrace();
            for (int j = 0; j < stack.length; j++) {
                for (int k = 0; k < j; k++)
                    System.out.print("  ");
                System.out.println(stack[j].getClassName() + "." + stack[j].getMethodName() + ":"
                        + stack[j].getLineNumber() + " <- ");
            }
        } else {
            System.out.print("null ");
        }
        System.out.flush();
    }
    System.out.println();
    System.out.flush();
    System.out.printf("==================== Thread Dump End ====================%n");
}

From source file:com.inmobi.grill.server.IndexResource.java

@GET
@Path("/admin/stack")
@Produces(MediaType.TEXT_PLAIN)//from  w w w  . j ava2s.c om
public String getThreadDump() {
    ThreadGroup topThreadGroup = Thread.currentThread().getThreadGroup();

    while (topThreadGroup.getParent() != null) {
        topThreadGroup = topThreadGroup.getParent();
    }
    Thread[] threads = new Thread[topThreadGroup.activeCount()];

    int nr = topThreadGroup.enumerate(threads);
    StringBuilder builder = new StringBuilder();
    builder.append("Total number of threads:").append(nr).append("\n");
    for (int i = 0; i < nr; i++) {
        builder.append(threads[i].getName()).append("\n\tState: ").append(threads[i].getState()).append("\n");
        String stackTrace = StringUtils.join(threads[i].getStackTrace(), "\n");
        builder.append(stackTrace);
        builder.append("\n----------------------\n\n");
    }
    return builder.toString();
}

From source file:com.dianping.dpsf.jmx.DpsfRequestorMonitor.java

private int getThreadCount(State state) {
    ThreadGroup threadGroup = clientManager.getClientResponseThreadPool().getFactory().getGroup();
    Thread[] threads = new Thread[threadGroup.activeCount()];
    threadGroup.enumerate(threads, false);
    int threadCount = 0;
    for (Thread t : threads) {
        if (state == t.getState()) {
            threadCount++;//from w  w  w. jav a 2  s. c  o  m
        }
    }
    return threadCount;
}

From source file:org.apache.pulsar.common.stats.JvmMetrics.java

private long getThreadCount() {
    // get top level thread group to track active thread count
    ThreadGroup parentThreadGroup = Thread.currentThread().getThreadGroup();

    while (parentThreadGroup.getParent() != null) {
        parentThreadGroup = parentThreadGroup.getParent();
    }/*from  w  w  w .  ja  v a 2 s.c  o  m*/

    return parentThreadGroup.activeCount();
}