Java ThreadGroup

2025 年 3 月 28 日 | 阅读 10 分钟

Java 提供了一种方便的方式将多个线程分组到一个对象中。这样,我们可以通过一次方法调用来挂起、恢复或中断一组线程。

注意:现在 suspend()、resume() 和 stop() 方法已弃用。

Java 线程组由 java.lang.ThreadGroup 类实现。

ThreadGroup 表示一组线程。线程组还可以包含其他线程组。线程组创建了一个树,除了初始线程组外,每个线程组都有一个父线程组。

线程可以访问有关其自身线程组的信息,但它无法访问有关其线程组的父线程组或任何其他线程组的信息。

ThreadGroup 类的构造函数

ThreadGroup 类只有两个构造函数。

编号。构造函数描述
1)ThreadGroup(String name)创建一个具有给定名称的线程组。
2)ThreadGroup(ThreadGroup parent, String name)创建一个具有给定父线程组和名称的线程组。

ThreadGroup 类的常用方法

ThreadGroup 类有许多方法。下面是 ThreadGroup 方法的列表。

序号修饰符和类型方法描述
1)voidcheckAccess()此方法确定当前运行的线程是否有权修改该线程组。
2)intactiveCount()此方法返回线程组及其子组中活动线程数量的估计值。
3)intactiveGroupCount()此方法返回线程组及其子组中活动线程组数量的估计值。
4)voiddestroy()此方法销毁线程组及其所有子组。
5)intenumerate(Thread[] list)此方法将线程组及其子组中的每个活动线程复制到指定的数组中。
6)intgetMaxPriority()此方法返回线程组的最大优先级。
7)StringgetName()此方法返回线程组的名称。
8)ThreadGroupgetParent()此方法返回线程组的父线程组。
9)voidinterrupt()此方法中断线程组中的所有线程。
10)booleanisDaemon()此方法测试该线程组是否为守护线程组。
11)voidsetDaemon(boolean daemon)此方法更改线程组的守护状态。
12)booleanisDestroyed()此方法测试该线程组是否已被销毁。
13)voidlist()此方法将线程组的信息打印到标准输出。
14)booleanparentOf(ThreadGroup g此方法测试该线程组是否是线程组参数或其祖先线程组之一。
15)voidsuspend()此方法用于挂起线程组中的所有线程。
16)voidresume()此方法用于恢复线程组中已通过 suspend() 方法挂起的线程。
17)voidsetMaxPriority(int pri)此方法设置组的最大优先级。
18)voidstop()此方法用于停止线程组中的所有线程。
19)StringtoString()此方法返回线程组的字符串表示形式。

让我们看一个对多个线程进行分组的代码示例。

现在,所有 3 个线程都属于一个组。这里,tg1 是线程组名,MyRunnable 是实现 Runnable 接口的类,而 "one"、"two" 和 "three" 是线程名。

现在,我们只需要一行代码就可以中断所有线程。

ThreadGroup 示例

文件:ThreadGroupDemo.java

输出

one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]

线程池方法示例:int activeCount()

让我们看看如何使用 activeCount() 方法。

文件名: ActiveCountExample.java

输出

Starting the first
Starting the second
The total number of active threads are: 2

线程池方法示例:int activeGroupCount()

现在,我们将学习如何在代码中使用 activeGroupCount() 方法。

文件名: ActiveGroupCountExample.java

输出

Starting the first
Starting the second
The total number of active thread groups are: 1
the second thread has finished executing
the first thread has finished executing

线程池方法示例:void destroy()

现在,我们将学习如何在代码中使用 destroy() 方法。

文件名: DestroyExample.java

输出

Starting the first
Starting the second
the first thread has finished executing
the second thread has finished executing
the child group is destroyed.
the parent group is destroyed.

线程池方法示例:int enumerate()

现在,我们将学习如何在代码中使用 enumerate() 方法。

文件名: EnumerateExample.java

输出

Starting the first
Starting the second
Thread the first is found.
Thread the second is found.
the first thread has finished executing
the second thread has finished executing

线程池方法示例:int getMaxPriority()

以下代码展示了 getMaxPriority() 方法的用法。

文件名: GetMaxPriorityExample.java

输出

Starting the first
Starting the second
The maximum priority of the parent ThreadGroup: 10
the first thread has finished executing
the second thread has finished executing

线程池方法示例:ThreadGroup getParent()

现在,我们将学习如何在代码中使用 getParent() 方法。

文件名: GetParentExample.java

输出

Starting the first
Starting the second
The ParentThreadGroup for the parent group is main
The ParentThreadGroup for the child group is the parent group
the first thread has finished executing
the second thread has finished executing

线程池方法示例:void interrupt()

以下程序说明了如何使用 interrupt() 方法。

文件名: InterruptExample.java

输出

Starting the first
Starting the second
The exception has been encountered java.lang.InterruptedException: sleep interrupted
The exception has been encountered java.lang.InterruptedException: sleep interrupted
the second thread has finished executing
the first thread has finished executing

线程池方法示例:boolean isDaemon()

以下程序说明了如何使用 isDaemon() 方法。

文件名: IsDaemonExample.java

输出

Starting the first
Starting the second
The group is not a daemon group.
the second thread has finished executing
the first thread has finished executing

线程池方法示例:boolean isDestroyed()

以下程序说明了如何使用 isDestroyed() 方法。

文件名: IsDestroyedExample.java

输出

Starting the first
Starting the second
The group has not been destroyed.
the first thread has finished executing
the second thread has finished executing