Kryten Kryten - 2 years ago 126
Java Question

Get a List of all Threads currently running in Java

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.

Answer Source

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()]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download