简体   繁体   English

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

[英]What is active thread group in Java?

There is method java.lang.ThreadGroup.activeGroupCount() returns an estimate of the number of active groups in a thread group.有一个方法 java.lang.ThreadGroup.activeGroupCount() 返回线程组中活动组数量的估计值。 In response to this question , the active thread is defined.针对这个问题,定义了活动线程。 But what does active thread group mean?但是活动线程组是什么意思呢?

In Java there is a group abstraction around a group of threads, so it is easier to manage a group of threads.在 Java 中,围绕一组线程有一个组抽象,因此管理一组线程更容易。 See eg Java: Thread Group参见例如Java:线程组

Every Java thread is a member of a thread group.每个 Java 线程都是线程组的成员。 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. Java 线程组由 java.lang 包中的 ThreadGroup(在 API 参考文档中)类实现。

As you noted, the terminology "active thread group" appears in the javadoc for ThreadGroup::activeGroupCount .正如您所指出的,术语“活动线程组”出现在ThreadGroup::activeGroupCountjavadoc中。

An active thread group is a ThreadGroup containing at least one active thread.一个活动线程组是一个至少包含一个活动线程的ThreadGroup

An active thread is one for which Thread::isAlive returns true .活动线程是Thread::isAlive返回true Thread::isAlive In other words, it has been started and has not yet terminated.换句话说,它已经开始并且还没有结束。


Note that thread groups are are only really suitable for debugging;请注意,线程组仅适用于调试; see What is the benefit of ThreadGroup in java over creating separate threads?请参阅java 中的 ThreadGroup 相对于创建单独的线程有什么好处? . . For example, the enumerate method has this javadoc caveat:例如, enumerate方法有这个javadoc警告:

"Due to the inherent race condition in this method, it is recommended that the method only be used for debugging and monitoring purposes." “由于此方法固有的竞争条件,建议该方法仅用于调试和监视目的。”

This also applies to the "count" methods.这也适用于“计数”方法。

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

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