java中的ThreadGroup相对于创建单独的线程有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1649133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:39:55  来源:igfitidea点击:

What is the benefit of ThreadGroup in java over creating separate threads?

javamultithreading

提问by sacsha

Many methods like stop(), resume(), suspend()etc are deprecated.

许多方法,如stop()resume()suspend()等已被弃用。

So is it useful to create threads using ThreadGroup?

那么使用创建线程有用ThreadGroup吗?

采纳答案by skaffman

Using ThreadGroupcan be a useful diagnostic technique in big application servers with thousands of threads. If your threads are logically grouped together, then when you get a stack trace you can see which group the offending thread was part of (e.g. "Tomcat threads", "MDB threads", "thread pool X", etc), which can be a big help in tracking down and fixing the problem.

ThreadGroup在具有数千个线程的大型应用程序服务器中使用可能是一种有用的诊断技术。如果您的线程在逻辑上分组在一起,那么当您获得堆栈跟踪时,您可以看到违规线程属于哪个组(例如“Tomcat 线程”、“MDB 线程”、“线程池 X”等),这可以是在追踪和解决问题方面有很大帮助。

回答by Andrzej Doyle

The short answer is - no, not really. There's little if any benefit to using one.

简短的回答是 - 不,不是真的。使用它几乎没有任何好处。

To expand on that slightly, if you want to group worker threads together you're much better off using an ExecutorService. If you want to quickly count how many threads in a conceptual group are alive, you still need to check each Thread individually (as ThreadGroup.activeCount() is an estimation, meaning it's not useful if the correctness of your code depends on its output).

稍微扩展一下,如果您想将工作线程组合在一起,最好使用 ExecutorService。如果您想快速计算概念组中有多少线程处于活动状态,您仍然需要单独检查每个线程(因为 ThreadGroup.activeCount() 是一个估计值,这意味着如果您的代码的正确性取决于其输出,则它没有用) .

I'd go so far as to say that the only thing you'd get from one these days, aside from the semantic compartmentalisation, is that Threads constructed as part of a group will pick up the daemon flag and a sensible name based on their group. And using this as a shortcut for filling in a few primitives in a constructor call (which typically you'd only have to writeonce anyway, sicne you're probably starting the threads in a loop and/or method call).

我什至要说,除了语义划分之外,这些天你唯一能得到的就是作为一个组的一部分构建的线程将根据它们获得守护进程标志和一个合理的名称团体。并使用它作为在构造函数调用中填充一些原语的快捷方式(通常您只需要编写一次,因为您可能正在循环和/或方法调用中启动线程)。

So - I really don't see any compelling reason to use one at all. I specifically tried to, a few months back, and failed.

所以 - 我真的没有看到任何令人信服的理由来使用一个。几个月前,我专门尝试过,但失败了。

EDIT - I supposeone potential use would be if you're running with a SecurityManager, and want to assert that only threads in the same group can interrupt each other. Even that's pretty borderline, as the default implementation always returns true for a Thread in anynon-system thread group. And if you're implementing your own SecurityManager, you've got the possibility to have it make its decision on any other criteria (including the typical technique of storing Threads in collections as they get created).

编辑 - 我一个潜在的用途是,如果您使用 SecurityManager 运行,并且想要断言只有同一组中的线程可以相互中断。即便如此,这也是非常接近的,因为对于任何非系统线程组中的线程,默认实现总是返回 true 。如果您正在实现自己的 SecurityManager,您就有可能让它根据任何其他标准做出决定(包括在创建线程时将它们存储在集合中的典型技术)。

回答by Greg Mattes

Don't use ThreadGroupfor new code. Use the Executorstuff in java.util.concurrentinstead.

不要ThreadGroup用于新代码。改用里面的Executor东西java.util.concurrent

回答by jbx

Somewhat complimentary to the answer provided (6 years ago or so). But, while the Concurrency API provides a lot of constructs, the ThreadGroupmight still be useful to use. It provides the following functionality:

对所提供的答案有些补充(大约 6 年前)。但是,虽然并发 API 提供了很多构造,但使用起来ThreadGroup可能仍然有用。它提供以下功能:

  1. Logical organisation of your threads (for diagnostic purposes).
  2. You can interrupt()all the threads in the group. (Interrupting is perfectly fine, unlike suspend(), resume()and stop()).
  3. You can set the maximum priority of the threads in the group. (not sure how widely useful is that, but there you have it).
  4. Sets the ThreadGroupas a daemon. (So all new threads added to it will be daemon threads).
  5. It allows you to override its uncaughtExceptionHandlerso that if one of the threads in the group throws an Exception, you have a callback to handle it.
  6. It provides you some extra tools such as getting the list of threads, how many active ones you have etc. Useful when having a group of worker threads, or some thread pool of some kind.
  1. 线程的逻辑组织(用于诊断目的)。
  2. 您可以interrupt()访问组中的所有线程。(与suspend(),resume()和不同,中断完全没问题stop())。
  3. 您可以设置组中线程的最大优先级。(不确定它有多大用处,但你有它)。
  4. 将 设置ThreadGroup为守护进程。(所以所有添加到它的新线程都将是守护线程)。
  5. 它允许您覆盖它,uncaughtExceptionHandler以便如果组中的一个线程抛出异常,您有一个回调来处理它。
  6. 它为您提供了一些额外的工具,例如获取线程列表、您有多少活动线程等。在拥有一组工作线程或某种线程池时很有用。

回答by Premraj

Great answerfor @skaffman. I want to add one more advantage:

@skaffman 的好答案。我想补充一个优势:

Thread groups helps manipulating all the threads which are defined in this at once.

线程组有助于立即操作在其中定义的所有线程。