如果您想获取当前线程的ThreadGroup,可以使用以下代码:

Thread.currentThread().getThreadGroup()

这是因为每个线程都有一个ThreadGroup,可以通过Thread类的静态方法currentThread()获取当前线程。

如果您想获取其他线程的ThreadGroup,可以使用Thread类的静态方法enumerate()获取所有线程,然后遍历每个线程并检查其ThreadGroup。

如果您需要在没有SecurityManager的情况下获取ThreadGroup,则可以使用以下代码:

ThreadGroup root = Thread.currentThread().getThreadGroup();
while (root.getParent() != null) {
    root = root.getParent();
}
Thread[] threads = new Thread[root.activeCount()];
root.enumerate(threads);

这将获取根ThreadGroup,并使用enumerate()方法获取所有线程

SecurityManager获取ThreadGroup的替代方案

原文地址: http://www.cveoy.top/t/topic/f5WO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录