官方解释

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链表设计

    链表 1,链表的实现 在实际开发之中对象数组是一项非常实用的技术,并且利用其可以描述出“多”方的概念,例如:一个人有多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于 ...

  2. 九、小程序 Redux详解与在小程序中怎么使用(action和reducers)

    什么是Redux ​ Redux我们可以把它理解成一个状态管理器,可以把状态(数据)存在Redux中,以便增.删.改.例如: 从服务器上取一个收藏列表,就可以把取回来的列表数据用Redux管理,多个页 ...

  3. 操作RDS文档说明

    操作RDS文档,让你对阿里云的知识更加了解.

  4. css图片上加文字

    第一种方法: 添加一个DIV,采用绝对定位,图片所属DIV为基准 <div style="position:relative;width:100px;height:100px;&quo ...

  5. Input system (输入子系统)

    Input system (输入子系统) 以前写一些输入设备(键盘,鼠标等)的驱动都是字符设备,混杂设备处理的,linux开源社区的大神门看到了这大量的输入设备如此分散不堪,就想有木有一种机制,可以对 ...

  6. [LeetCode] 283. Move Zeroes ☆(移动0到最后)

    描述 给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置. 举例: nums = [0, 1, 0, 3, 12], 函数运行后结果为[1, 3, 12, ...

  7. python 爬虫抓取 MOOC 中国课程的讨论区内容

    一:selenium 库 selenium 每次模拟浏览器打开页面,xpath 匹配需要抓取的内容.可以,但是特别慢,相当慢.作为一个对技术有追求的爬虫菜鸡,狂补了一些爬虫知识.甚至看了 scrapy ...

  8. Django:总结setting中的配置

    一.Django setting配置说明 二.setting配置一览 一.Django setting配置说明 1.基础 DJANGO_SETTING_MODULE环境变量:让settings模块被包 ...

  9. No result defined for action com.java.test.Action.HelloAction and result index

    Struts中配置action访问出错: Struts Problem Report Struts has detected an unhandled exception: Messages: No ...

  10. Paper Reading:ION

    Inside-Outside Net (ION) 论文:Inside-Outside Net: Detecting Objects in Context with Skip Pooling and R ...