简体   繁体   English

知道ThreadGroup在Java中有用吗?

[英]Is knowing the ThreadGroup useful in Java?

The Java Programming Language uses ThreadGroup to manage the threads access rights to each other: 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. 允许线程访问有关其自己的线程组的信息,但不允许访问有关其线程组的父线程组或任何其他线程组的信息。

I think I understand it, but is this useful in any way? 我想我理解,但这有什么用吗? I wonder whether revealing the current ThreadGroup structure and its elements, thus the threads and other ThreadGroup, hold any information from a debugging perspective. 我想知道从调试的角度来看,揭示当前的ThreadGroup结构及其元素(即线程和其他ThreadGroup)是否包含任何信息。

I have found it handy although it's quite bugged. 我发现它很方便,尽管它已经很麻烦了。 It can be useful for providing management content b/c the threads created will be added to the parent thread's ThreadGroup (and can be efficiently enumerated). 对于提供管理内容b / c很有用,创建的线程将被添加到父线程的ThreadGroup中(并且可以被有效地枚举)。 It can be used to prevent new Thread spawning (but it's not documented or intended). 它可以用来防止新的Thread产生(但是没有记录或有意提供)。

Unfortunately ThreadGroup has quite a few bugs, incl. 不幸的是,ThreadGroup有很多错误,包括。 destroy() method. destroy()方法。

In short, if you are not going to write middleware, don't bother. 简而言之,如果您不打算编写中间件,请不要打扰。 If you need to enumerate all the threads in an application use Thread.getAllStackTraces().keySet() , the method has a security hole though but I am not discussing this part. 如果您需要使用Thread.getAllStackTraces().keySet()枚举应用程序中的所有线程,尽管该方法存在安全漏洞,但我不在此讨论。

Edit: I missed the debugging aspect: ThreadGroups are actually useful for debugging purposes if the debugging tools allows for tree-view and grouping by the ThreadGroup (for example eclipse does so). 编辑:我错过了调试方面:如果调试工具允许通过ThreadGroup进行树形视图和分组,则ThreadGroup实际上对于调试有用。 Provided you use it properly, all your threads in the currently debugged module/mbean/etc should be in the same group. 如果正确使用它,则当前已调试模块/ mbean / etc中的所有线程应位于同一组中。 It allows easier tracking of thread leaks (compared to looking at hundred other threads) 它可以更轻松地跟踪线程泄漏(与查看其他一百个线程相比)

ThreadGroup is generally considered a failed experiment. ThreadGroup通常被认为是失败的实验。 If your code needs to interact with the Java PlugIn or Java WebStart environments, then knowing about it is useful. 如果您的代码需要与Java PlugIn或Java WebStart环境进行交互,那么了解它会很有用。 Otherwise, I wouldn't really bother. 否则,我真的不会打扰。

First, this is pretty much moot. 首先,这几乎没有什么意义。 Very few applications make use of thread groups, so application threads nearly always end up in the same thread group. 很少有应用程序使用线程组,因此应用程序线程几乎总是以同一线程组结尾。

Second, I think that restriction you quoted is referring to sandboxed applications. 其次,我认为您引用的限制是指沙盒应用程序。 According to other parts of the ThreadGroup javadoc, the ability to enumerate the threads in a thread thread group is controlled by the security manager. 根据ThreadGroup javadoc的其他部分,枚举线程线程组中的线程的能力由安全管理器控制。

Thirdly, if an application is sandboxed, it is always a good idea to restrict its knowledge of stuff outside of the sandbox. 第三,如果应用程序是沙盒,最好将其知识范围限制在沙盒之外。 If not, well there's not much to hide. 如果没有,那么隐藏的东西就不多了。

Finally, if you are using a debugger it will have access to the root thread group, and hence won't be hampered by this restriction ... if it really exists. 最后,如果您使用的是调试器,它将有权访问根线程组,因此不会受到此限制的阻碍……如果确实存在。 Similarly, the "main" thread will be in the root thread group. 同样,“主”线程将位于根线程组中。


As @Tom Hawtin says, thread groups are regarded as a failed experiment, especially since the methods that they are designed to support (Thread.stop, Thread.destroy, etc) have been deprecated on account of their being unsafe. 正如@Tom Hawtin所说,线程组被认为是失败的实验,尤其是因为线程组的安全性已被弃用,因为线程组旨在支持的方法(Thread.stop,Thread.destroy等)已被弃用。

ThreadGroups can make thread management a little easier, in a case when you must deal with large number of threads... 在必须处理大量线程的情况下,线程组可以使线程管理更容易一些。

Btw, article explaining ThreadGroups at JavaWorld: http://www.javaworld.com/javaworld/jw-08-2002/jw-0802-java101.html 顺便说一句,在JavaWorld上解释线程组的文章: http : //www.javaworld.com/javaworld/jw-08-2002/jw-0802-java101.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM