我想知道为什么互联网上关于线程组的文档这么少? 它们是否仍在使用或者它们是一些陈旧的概念? 有人可以解释一下吗:
它们是什么。
它们的用途。
如果它们仍在使用,在哪里?
给出一些真实的应用示例(可能是 Web 服务器之类的)。
最佳答案
它们被用作一组线程。在一个简单的应用程序中,您只需要一个,但在一个更复杂的应用程序服务器中,每个应用程序都需要一个。
why there is so little documentation about Thread Groups on the internet ?
我想有些人认为这是一个非常简单的想法。不确定它缺少什么。
Are they still used or they are some stale concept ?
我认为大多数开发人员从未考虑过线程组。但我认为它们在某些情况下很有用。我们有一个库,其中有一个用于重置线程关联的自定义线程组。
Can some one explain that are they what they are used for, if still used, and give an example.
主要是在应用程序服务器中,每个服务器都有自己的线程集合,可以集中管理。如果您想监视或关闭应用程序,您需要知道该应用程序启动了哪些线程。
如果您在 ThreadGroup 中启动一个线程,则它创建的每个 Thread 也将在该线程组中。如果没有此功能,您将很难为应用程序分配线程。
From @biziclop: How do you reliably enumerate threads in a group?
您可以获得 activeThreads 的大小并在 ThreadGroup 锁定在 this
时枚举(无论好坏)
synchronized(threadGroup) {
Thread[] threads = threadGroup.activeCount();
threadGroup.enumerate(threads);
// use threads before the lock is released or it could be wrong.
}
关于java - 什么是 Java 中的线程组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36157879/