一、什么是线程组
线程组(ThreadGroup)就是由线程组成的管理线程的类,这个类是java.lang.ThreadGroup类。
在Java中每一个线程都归属于某个线程组管理的一员,例如在主函数main()主工作流程中产生一个线程,则产生的线程属于main这个线程组管理的一员。
A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.
A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups.
线程组表示一组线程。此外,线程组还可以包括其他线程组。线程组形成一个树,其中除了初始线程组之外的每个线程组都有一个父线程。
允许线程访问有关其自身线程组的信息,但不能访问有关其线程组的父线程组或任何其他线程组的相关信息。
二、源码分析
2.1 ThreadGroup Diagrams

2.2 属性

// 父线程组
private final ThreadGroup parent;
// 线程组名称
String name;
// 线程组最大优先级
int maxPriority;
// 销毁状态
boolean destroyed;
// 守护线程组表示: true-守护线程组;false-普通线程组
boolean daemon;
// 虚拟机自动挂起【属性是一个标识符,用于表示当前线程组中的所有线程是否可以被挂起。如果 vmAllowSuspension 属性为 true,则当前线程组中的所有线程可以被挂起;如果 vmAllowSuspension 属性为 false,则当前线程组中的所有线程不可以被挂起。默认情况下,vmAllowSuspension 属性是 true,即所有线程可以被挂起】
boolean vmAllowSuspension;
// 未启动线程数(EW状态的线程数)
int nUnstartedThreads = 0;
// 线程总数【仅统计当前线程组中的活动线程数量,不包括子线程组中的线程数量】
int nthreads;
// 线程数组【表示当前线程组中的所有线程,它是一个 Thread 类型的数组,包含了当前线程组中正在运行或等待运行的所有线程】
Thread threads[];
// 线程组数量,【仅统计当前线程组的子线程组数量,不包括子线程组的子线程组数量】
int ngroups;
// 线程组数组
ThreadGroup groups[];
2.3 构造函数

