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

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

    Thread[] list = new Thread[pGroup.activeCount()];
    int count = pGroup.enumerate(list, true);
    for (int i = 0; i < count; i++) {
        System.out.println("Thread " + list[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 w w w . jav  a2s  .c  om

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

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

}

From source file:test.concurrency.TestApplicationScope.java

public void testApplicationScope() throws Exception {
    int threads = 50;
    int reps = 10;

    ThreadGroup group = new ThreadGroup("TestThreads");

    for (int i = 0; i < threads; i++) {
        TestRunnable tr = new TestRunnable(reps);
        Thread thread = new Thread(group, tr, "TestThread #" + i);
        thread.start();//from w  ww .ja  v  a 2  s  .c o m
    }

    while (group.activeCount() > 0 && error == null) {
        Thread.sleep(100);
    }

    if (error != null) {
        throw error;
    }
}

From source file:org.apache.falcon.resource.admin.AdminResource.java

@GET
@Path("stack")
@Produces(MediaType.TEXT_PLAIN)/* ww  w  . j av a2 s. c  o m*/
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();
    for (int i = 0; i < nr; i++) {
        builder.append(threads[i].getName()).append("\nState: ").append(threads[i].getState()).append("\n");
        String stackTrace = StringUtils.join(threads[i].getStackTrace(), "\n");
        builder.append(stackTrace);
    }
    return builder.toString();
}

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

private int getThreadCount(RequestProcessor requestProcessor, State state) {
    ThreadGroup threadGroup = requestProcessor.getThreadPool().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. java2 s . co m
        }
    }
    return threadCount;
}

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

private String getThreadStackTraces(RequestProcessor requestProcessor, State state, int threadCount) {
    ThreadGroup threadGroup = requestProcessor.getThreadPool().getFactory().getGroup();
    Thread[] threads = new Thread[threadGroup.activeCount()];
    threadGroup.enumerate(threads, false);
    StringBuilder builder = new StringBuilder();
    int count = 0;
    if (threads != null && threads.length > 0 && threadCount > 0) {
        for (Thread thread : threads) {
            if (state == thread.getState()) {
                count++;/*from w ww.j  a va  2s .com*/
                if (count > 1) {
                    builder.append("\r\n\r\n");
                }
                builder.append("Thread ").append(thread.getId()).append("  ").append(thread.getName())
                        .append(" (state = ").append(state).append(")").append("\r\n");
                StackTraceElement[] stackTrace = thread.getStackTrace();
                for (StackTraceElement ste : stackTrace) {
                    builder.append(ste.getClassName()).append("-").append(ste.getMethodName()).append("(")
                            .append(ste.getLineNumber()).append(")").append("\r\n");
                }
                if (count >= threadCount) {
                    break;
                }
            }
        }
    }
    return builder.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();// ww  w .  j a  va2s.c om

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

    System.out.println("Active threads in " + pGroup.getName() + " = " + pGroup.activeCount());

    System.out.println("Is " + pGroup.getName() + " a daemon ThreadGroup? " + pGroup.isDaemon());
    System.out.println("Is " + cGroup.getName() + " a daemon ThreadGroup? " + cGroup.isDaemon());
}

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  av  a2 s  .  com

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

    System.out.println("Active threads in \"" + pGroup.getName() + "\" = " + pGroup.activeCount());

}

From source file:org.codelibs.fess.servlet.Tomcat6ConfigServlet.java

private Thread[] getThreads() {
    // Get the current thread group
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    // Find the root thread group
    while (tg.getParent() != null) {
        tg = tg.getParent();/* w w  w  .java2 s  .co m*/
    }

    int threadCountGuess = tg.activeCount() + 50;
    Thread[] threads = new Thread[threadCountGuess];
    int threadCountActual = tg.enumerate(threads);
    // Make sure we don't miss any threads
    while (threadCountActual == threadCountGuess) {
        threadCountGuess *= 2;
        threads = new Thread[threadCountGuess];
        // Note tg.enumerate(Thread[]) silently ignores any threads that
        // can't fit into the array
        threadCountActual = tg.enumerate(threads);
    }

    return threads;
}

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 w w  w .  j ava  2 s.co  m

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

    System.out.println("Active group(child) threads in \"" + pGroup.getName() + "\" = " + pGroup.activeCount());

}