获取当前在 Java 中运行的所有线程的列表

问题描述:

有什么方法可以获得当前 JVM 中所有正在运行的线程的列表(包括我的类启动的线程)?

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

是否也可以获取列表中所有线程的ThreadClass对象?

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

我希望能够通过代码做到这一点.

获取可迭代集:

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

性能:12 个线程为 0 毫秒(Azul JVM 16.0.1、Windows 10、Ryzen 5600X).

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