Java Thread Group 怎么用?Thread Group 如何创建和管理线程组?

文章导读
上一个 测验 下一个 ThreadGroup 类 Java ThreadGroup 类表示一组线程。它还可以包含其他 thread group。thread group 形成一棵树,除了初始 thread group 之外,每个 thread group 都有一个父组。
📋 目录
  1. ThreadGroup 类
  2. ThreadGroup 类声明
  3. ThreadGroup 类构造方法
  4. ThreadGroup 类方法
  5. 继承的方法
  6. Java 中 ThreadGroup 类的示例
A A

Java - ThreadGroup 类



上一个
测验
下一个

ThreadGroup 类

Java ThreadGroup 类表示一组线程。它还可以包含其他 thread group。thread group 形成一棵树,除了初始 thread group 之外,每个 thread group 都有一个父组。

ThreadGroup 类声明

以下是 java.lang.ThreadGroup 类的声明 −

public class ThreadGroup
   extends Object
      implements Thread.UncaughtExceptionHandler

ThreadGroup 类构造方法

序号 构造方法 & 描述
1

ThreadGroup(String name)

此构造方法创建一个新的 thread group。

2

ThreadGroup(ThreadGroup parent, String name)

此构造方法创建一个新的 thread group。

ThreadGroup 类方法

序号 方法 & 描述
1 int activeCount()

此方法返回此 thread group 中活动线程数量的估计值。

2 int activeGroupCount()

此方法返回此 thread group 中活动组数量的估计值。

3 void checkAccess()

此方法确定当前运行的 thread 是否有权限修改此 thread group。

4 void destroy()

此方法销毁此 thread group 及其所有子组。

5 int enumerate(Thread[] list)

此方法将此 thread group 及其子组中的每个活动 thread 复制到指定的数组中。

6 int enumerate(Thread[] list, boolean recurse)

此方法将此 thread group 中的每个活动 thread 复制到指定的数组中。

7 int enumerate(ThreadGroup[] list)

此方法将此 thread group 中每个活动子组的引用复制到指定的数组中。

8 int enumerate(ThreadGroup[] list, boolean recurse)

此方法将此 thread group 中每个活动子组的引用复制到指定的数组中。

9 int getMaxPriority()

此方法返回此 thread group 的最大优先级。

10 String getName()

此方法返回此 thread group 的名称。

11 ThreadGroup getParent()

此方法返回此 thread group 的父组。

12 void interrupt()

此方法中断此 thread group 中的所有 thread。

13 boolean isDaemon()

此方法测试此 thread group 是否为守护 thread group。

14 boolean isDestroyed()

此方法测试此 thread group 是否已被销毁。

15 void list()

此方法将此 thread group 的信息打印到标准输出。

16 boolean parentOf(ThreadGroup g)

此方法测试此 thread group 是否为参数 thread group 或其祖先 thread group 之一。

17 void setDaemon(boolean daemon)

此方法更改此 thread group 的守护状态。

18 void setMaxPriority(int pri)

此方法设置该组的最大优先级。

19 String toString()

此方法返回此 Thread group 的字符串表示。

20 void uncaughtException(Thread t, Throwable e)

当此 thread group 中的 thread 因未捕获异常而停止,且该 thread 未安装特定的 Thread.UncaughtExceptionHandler 时,Java 虚拟机会调用此方法。

继承的方法

此类从以下类继承方法 −

  • java.lang.Object

Java 中 ThreadGroup 类的示例

以下示例展示了在多线程程序中使用 ThreadGroup 的用法。

package com.;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.func();
   }

   public void func() {
      try {     
         // 创建一个父 ThreadGroup
         ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");
    
         // 为父 ThreadGroup 创建一个子 ThreadGroup
         ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

         // 创建一个线程
         Thread t1 = new Thread(pGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // 创建另一个线程
         Thread t2 = new Thread(cGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // 显示活动线程的数量 
         System.out.println("Active threads in \"" + pGroup.getName() 
            + "\" = " + pGroup.activeCount());

         // 阻塞直到其他线程完成
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // 实现 run()
   public void run() {

      for(int i = 0;i < 1000;i++) {
         i++;
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果 −

Starting Thread-0...
Starting Thread-1...
Thread-0 finished executing.
Active threads in "Parent ThreadGroup" = 1
Thread-1 finished executing.