官方解释

public class ThreadGroup
extends Object
implements Thread.UncaughtExceptionHandler
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.
当在main方法中未指定一个线程的名字和对应的线程组的名称时,jvm会自动创建一个main线程,该线程组的名称也为main
System.out.println(Thread.currentThread().getName());//main
System.out.println(Thread.currentThread().getThreadGroup().getName());//main

一个线程组可以成为另一个线程组的父线程组

       ThreadGroup tg1 = new ThreadGroup("TG1");
Thread t1 = new Thread(tg1, "t1") {
@Override
public void run() {
try {
//TG1
System.out.println(getThreadGroup().getName());
//java.lang.ThreadGroup[name=main,maxpri=10]
System.out.println(getThreadGroup().getParent());
System.out.println(getThreadGroup().getParent().activeCount());
Thread.sleep(10_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start(); ThreadGroup tg2 = new ThreadGroup(tg1, "TG2");
//TG2
System.out.println(tg2.getName());
//java.lang.ThreadGroup[name=TG1,maxpri=10]
System.out.println(tg2.getParent());

线程组.activeCount()可以获取当前线程组以及子线程组此时活着的线程

     ThreadGroup tg1 = new ThreadGroup("TG1");
Thread t1 = new Thread(tg1, "t1") {
@Override
public void run() {
try {
Thread.sleep(10_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start(); ThreadGroup tg2 = new ThreadGroup(tg1, "TG2");
Thread t2 = new Thread(tg2, "t2") {
@Override
public void run() {
//TG1
System.out.println(tg1.getName());
Thread[] threads = new Thread[tg1.activeCount()];
tg1.enumerate(threads);
//Thread[t1,5,TG1]
//Thread[t2,5,TG2]
Arrays.asList(threads).forEach(System.out::println);
}
};
t2.start();

enumerate

public int enumerate(Thread[] list,
boolean recurse)
Copies into the specified array every active thread in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group and references to every active thread in these subgroups are also included. If the array is too short to hold all the threads, the extra threads are silently ignored.

An application might use the activeCount method to get an estimate of how big the array should be, however if the array is too short to hold all the threads, the extra threads are silently ignored. If it is critical to obtain every active thread in this thread group, the caller should verify that the returned int value is strictly less than the length of list.

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

Parameters:
list - an array into which to put the list of threads
recurse - if true, recursively enumerate all subgroups of this thread group
Returns:
the number of threads put into the array
Throws:
SecurityException - if checkAccess determines that the current thread cannot access this thread group
enumerate没有入参时默认是复制该线程组及子线程组的活着的线程,等同于enumerate(Thread[] list, boolean recurse)中recurse为true,
enumerate(Thread[] list, boolean recurse)中recurse为false时只复制当前线程组的线程
        ThreadGroup tg1 = new ThreadGroup("TG1");
Thread t1 = new Thread(tg1, "t1") {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.start(); ThreadGroup tg2 = new ThreadGroup(tg1, "TG2");
Thread t2 = new Thread(tg2, "t2") {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t2.start();
System.out.println("======================");
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
Thread[] threads = new Thread[tg1.activeCount()];
tg1.enumerate(threads, false);
Arrays.asList(threads).forEach(System.out::println);

setDaemon

public final void setDaemon(boolean daemon)
Changes the daemon status of this thread group.

First, the checkAccess method of this thread group is called with no arguments; this may result in a security exception.

A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.

Parameters:
daemon - if true, marks this thread group as a daemon thread group; otherwise, marks this thread group as normal.
Throws:
SecurityException - if the current thread cannot modify this thread group.
如果设置当前线程组为守护模式,在当前线程组最后一个线程或者线程组被销毁时自动销毁当前线程组,否则需要手动 destroy()
     ThreadGroup tg1 = new ThreadGroup("TG1");
Thread t1 = new Thread(tg1, "t1") {
@Override
public void run() {
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
tg1.setDaemon(true);
t1.start(); try {
Thread.sleep(2_000);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println(tg1.isDestroyed());

ThreadGroupAPI的更多相关文章

  1. java线程基础巩固---ThreadGroup API学习

    ThreadGroup初识: 这次来学习一个新的线程概念---线程组(ThreadGroup),首先从JDK文档中对它进行一个大致的了解,如下: 下面开始用代码来进行说明,对于一个线程来说如果没有指定 ...

随机推荐

  1. Java 实现简单的 RPC 框架

    RPC 简介 RPC,全称为 Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式,而不需要了解底层网络技术 ...

  2. javamail "535 5.7.3 Authentication unsuccessful" 问题排查

    有一家odm的服务器用Javamail发邮件的时候报错  Authentication unsuccessful  其他的有些又是正常的 网上查了一下解决方法如下 JavaMailSenderImpl ...

  3. 转:applicationContext.xml文件放置位置不同而导致的jUnit测试的时候路径的不同

    如果applicationContext.xml文件放置在src下面的的时候使用jUint测试的时候编写的路径应该是这样的: @Test public void saveTest() { Applic ...

  4. sql server 某一列求和

    sql server 某一列求和 SELECT 患者来源,设备类型,检查部位,设备名称,convert(char(10),STUDY_DATE,121) as 日期, count(distinct 就 ...

  5. 【已解决】如图,说我磁盘不够,看到var目录下有的个隐藏文件夹占了46G,不知道怎么删除

    后来发现不是隐藏目录,是其中的log目录,然后一步一步往下,找到jenkins.log文件,已经有40+G的log了.

  6. idou老师教你学Istio12 : Istio 实现流量镜像

    微服务为我们带来了快速开发部署的优秀特性,而如何降低开发和变更的风险成为了一个问题.Istio的流量镜像,也称为影子流量,是将生产流量镜像拷贝到测试集群或者新的版本中,在引导实时流量之前进行测试,可以 ...

  7. windows7重置网卡命令

    点击windows左下角菜单键,输入cmd 然后鼠标右键 cmd 以管理员身份运行,并输入命令 netsh winsock reset, 然后回车执行. 系统会提示要求重启计算机. 重启后即重置网卡成 ...

  8. 系统---添加一个相机的IP实现opencv读取rstp视频流

    系统---添加一个相机的IP实现opencv读取视频流 这里: 第一步,添加一个6段的相机IP地址:先ping 一个IP地址,ping通的是再用的IP,不可添加:ping不通的,添加IP到网络. 第二 ...

  9. SmtpClient 发送邮件

    利用SmtpClient 代码发送邮件. 简单测试代码: static void Main(string[] args) { MailMessage msg = new MailMessage(); ...

  10. vue param和query两种传参方式

    1.传参方式 query传参方式 this.$router.push({ path: "/home", query: {code:"123"} }) param ...