views:

2329

answers:

7

Hi

Is there any way I can get a list of all the running Threads in the current JVM (including the Threads NOT started by my class)?

Is it also possible to get the Thread and Class objects of all Thread in the list?

I want to be able to do this through code.

+3  A: 

Yes, take a look at getting a list of threads. Lots of examples on that page.

That's to do it programmatically. If you just want a list on Linux at least you can just use this command:

kill -3 processid

and the VM will do a thread dump to stdout.

cletus
kill -3? At least on my linux, that's "terminal quit". Kills, does not list.
khedron
+1  A: 

Have you taken a look at jconsole?

This will list all threads running for a particular Java process.

You can start jconsole from the JDK bin folder.

You can also get a full stack trace for all threads by hitting Ctrl+Break in Windows or by sending kill pid --QUIT in Linux.

pjp
I want to access the list within my java class
Kryten
In which case look at cletus' answer.
pjp
Um, why are people voting this up when the guy said he wanted a programmatic solution?
cletus
Because the question doesn't state that. I'll edit the question to make it explicit.
pjp
+5  A: 

Get a handle to the root ThreadGroup, like this:

ThreadGroup rootGroup = Thread.currentThread( ).getThreadGroup( );
ThreadGroup parentGroup;
while ( ( parentGroup = rootGroup.getParent() ) != null ) {
    rootGroup = parentGroup;
}

Now, call the enumerate() function on the root group repeatedly. The second argument lets you get all threads, recursively:

Threads[] threads = new Thread[ rootGroup.activeCount() ];
while ( rootGroup.enumerate( threads, true ) == threads.length ) {
    threads = new Thread[ threads.length * 2 ];
}

Note how we call enumerate() repeatedly until the array is large enough to contain all entries.

Frerich Raabe
A: 

In the java console, hit Ctrl-Break. It will list all threads plus some information about the heap. This won't give you access to the objects of course, as this is not a programmatic. But it can be very helpful for debugging anyway.

raoulsson
+1  A: 

You can get a lot of information about threads from the ThreadMXBean.

Call the static ManagementFactory.getThreadMXBean() method to get a reference to the MBean.

Dan Dyer
A: 
    public static void main(String[] args) {


     // Walk up all the way to the root thread group
        ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup parent;
        while ((parent = rootGroup.getParent()) != null) {
            rootGroup = parent;
        }

        listThreads(rootGroup, "");
    }


    // List all threads and recursively list all subgroup
    public static void listThreads(ThreadGroup group, String indent) {
        System.out.println(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];
            System.out.println(indent + "  Thread[" + t.getName() 
              + ":" + t.getClass() + "]");
        }

        // 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++) {
            listThreads(groups[i], indent + "  ");
        }
    }
ZZ Coder
A: 

To get an iterable set:

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

To convert it to an array:

Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
thejoshwolfe