java - 了解 Java 中的 ThreadGroup 有用吗?

标签 java multithreading debugging thread-safety

Java 编程语言使用 ThreadGroup 来管理线程之间的访问权限:

A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.

A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups.

我想我明白了,但这有什么用处吗?我想知道从调试的角度来看,是否揭示当前的 ThreadGroup 结构及其元素,从而线程和其他 ThreadGroup 保存有任何信息。

最佳答案

我发现它很方便,尽管它有很多问题。它对于提供管理内容很有用,因为创建的线程将被添加到父线程的 ThreadGroup 中(并且可以有效地枚举)。它可用于防止新线程产生(但没有记录或有意为之)。

不幸的是,ThreadGroup 有很多错误,包括。 destroy() 方法。

简而言之,如果您不打算编写中间件,请不要打扰。如果您需要使用 Thread.getAllStackTraces().keySet() 枚举应用程序中的所有线程,该方法虽然存在安全漏洞,但我不讨论这部分。

编辑:我错过了调试方面: 如果调试工具允许按 ThreadGroup 进行 TreeView 和分组(例如 eclipse 就这样做),那么 ThreadGroup 实际上对于调试目的很有用。如果您正确使用它,当前调试的模块/mbean/等中的所有线程都应该位于同一组中。它可以更轻松地跟踪线程泄漏(与查看数百个其他线程相比)

关于java - 了解 Java 中的 ThreadGroup 有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6440049/

相关文章:

java - FileDialog 中不必要的构造函数?

multithreading - 在 S3 存储桶之间并行移动文件

java - 如何使用处理在单独的线程中读取 OpenGL 像素?

objective-c - 如何根据对象字符串属性在 Xcode 中设置条件断点?

debugging - R 调试 : "only 0' s may be mixed with negative subscripts"

来自终端奇怪行为的 Java 输入

Java Lambda 多次求和

java - 数组声明和集合声明有什么区别

Ruby 的线程速度

debugging - Mongoose 调试日志到单独的文件