获取当前在 Java 中运行的所有线程的列表 [英] Get a list of all threads currently running in Java

查看:23
本文介绍了获取当前在 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).

这篇关于获取当前在 Java 中运行的所有线程的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆