Get a list of all threads currently running in Java

java, jvm, multithreading

Solution

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).

Problem

Is there any way I can get a list of all 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 threads in the list? I want to be able to do this through code.

Original source