Java 中的活动线程组是什么?

What is active thread group in Java?

有方法 java.lang.ThreadGroup.activeGroupCount() returns 估计线程组中活动组的数量。响应this question,活动线程被定义。但是活跃线程组是什么意思呢?

在Java中有一个围绕一组线程的组抽象,因此更容易管理一组线程。参见例如Java: Thread Group

Every Java thread is a member of a thread group. Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually.

For example, you can start or suspend all the threads within a group with a single method call. Java thread groups are implemented by the ThreadGroup(in the API reference documentation) class in the java.lang package.

如您所述,ThreadGroup::activeGroupCountjavadoc 中出现了术语 "active thread group"。

活动线程组是 ThreadGroup 至少包含一个活动线程。

活动线程是 Thread::isAlive returns true 的线程。也就是说已经开始了,还没有结束。


请注意,线程组仅适用于调试;参见 What is the benefit of ThreadGroup in java over creating separate threads?. For example, the enumerate method has this javadoc 注意事项:

"Due to the inherent race condition in this method, it is recommended that the method only be used for debugging and monitoring purposes."

这也适用于 "count" 方法。