Pawan
Pawan

Reputation: 32331

How to know how many threads have been created and running?

This is my simple program in Java:

public class Counter extends Thread {

    public static void main(String args[]) {    
        Thread t1 = new Thread();
        Thread t2 = new Thread();
        t1.start();
        t2.start();
    }
}

I am using Windows Operating System 32-bit. My question is, how can we know how many Threads are created in the program and how many Threads are running? Is there any such tool?

Upvotes: 24

Views: 31303

Answers (5)

Ravindra babu
Ravindra babu

Reputation: 38950

You can get a set of running threads from Thread class with getAllStackTraces() method. Iterate that set and print current status using getState() API.

Sample code:

import java.util.Set;

public class ThreadSet {
    public static void main(String args[]){
        Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
        for ( Thread t : threadSet){
            System.out.println("Thread :"+t+":"+"state:"+t.getState());
        }
    }

}

If you expect only your Thread, which calls main will be listed in output, you will be surprised.

output:

java ThreadSet

Thread :Thread[Finalizer,8,system]:state:WAITING
Thread :Thread[main,5,main]:state:RUNNABLE
Thread :Thread[Reference Handler,10,system]:state:WAITING
Thread :Thread[Signal Dispatcher,9,system]:state:RUNNABLE
Thread :Thread[Attach Listener,5,system]:state:RUNNABLE

Upvotes: 3

Andrejs
Andrejs

Reputation: 27737

Thread.getAllStackTraces() will give you a map where each Thread is key. You can then examine the state of each Thread and check thread.isAlive().

Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();

Upvotes: 11

alain.janinm
alain.janinm

Reputation: 20065

I use this method using ThreadMXBean if you want the threads themselves:

 /**
 * Return an array with all current threads.
 * @return  Thread[] - array of current Threads
 */
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);
}

/**
 * Get current ThreadGroup.
 * @see getAllThreads()
 * @return
 */
ThreadGroup getRootThreadGroup(){
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    ThreadGroup ptg;
    while ((ptg=tg.getParent())!=null){
        tg = ptg;
    }
    return tg;
}

Upvotes: 5

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9317

You can access all available information about threads in your program using: http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html

If you just need a tool for this you can use jconsole, jvisualvm and may other profiling tools which can show you details of the running threads in a gui.

Upvotes: 5

dexametason
dexametason

Reputation: 1133

System.out.println("Number of active threads from the given thread: " + Thread.activeCount());

Upvotes: 22

Related Questions