官方解释

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 MergeSort

    Java MergeSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  2. Django rest-framework框架-组件之视图

    视图: a. django class Test(View): ... b. rest_framework class Test(APIView): ... c. GenericAPIView 一般不 ...

  3. B树Java代码实现以及测试

    B树定义 B 树又叫平衡多路查找树.一棵m阶的B 树 (m叉树)的特性如下: 根节点至少有两个孩子 每个非根节点至少有M/2(上取整)个孩子,至多有M个孩子. 每个非根节点至少有M/2-1(上取整)个 ...

  4. 页面使用element-tree

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Vue 将一个组件嵌入到另一个组件中

    https://github.com/JasmineQian/Vue_Sample App.vue是所有组件的 要嵌入到App.vue组件中, 在script处导入 import xxx  from ...

  6. js中对new Date() 中转换字符串方法toLocaleString的使用

    提供特定于区域设置的日期和时间格式. dateTimeFormatObj = new Intl.DateTimeFormat([locales][, options]) dateTimeFormatO ...

  7. Ubuntu系统---安装English版本之后的一些工作

                                                                      Ubuntu系统---安装English版本之后的一些工作 安装完U ...

  8. split()函数实现

    #split函数实现: ss='** *axx* *bv** *ctt** **dff***' result=[] def split_1(ss,a,times=len(ss)): i=0 n=0 w ...

  9. 从c到c++<四>

    总结一下:内联函数实际上就是用inline修饰的函数,这些函数会在编译时由编译器来将代码展开,而不用像上面第二点提到的人工展开,它的使用场景:代码很短.使用频率高. 具体代码如下: 对于这两者实际上还 ...

  10. 51Nod - 1714 B君的游戏

    每个数的SG值之和他有多少个1相关 打表复杂度:找K个有序的<n的非负数的复杂度为nk/(k!) 则这题的SG打表复杂度为648/7! 为1e10左右 void dfs(int cur, int ...