Get a list of all threads currently running in Java Get a list of all threads currently running in Java multithreading multithreading

Get a list of all threads currently running in Java


To get an iterable set:

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

Performance: 0 ms for 12 threads (Azul JVM 16.0.1, Windows 10, Ryzen 5600X).


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:

Thread[] 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.


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.