ThreadGroupAPI
官方解释
public class ThreadGroup
extends Object
implements Thread.UncaughtExceptionHandlerA 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.
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. Ifrecurseistrue, 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 threadsrecurse- iftrue, 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
checkAccessmethod 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- iftrue, 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的更多相关文章
- java线程基础巩固---ThreadGroup API学习
ThreadGroup初识: 这次来学习一个新的线程概念---线程组(ThreadGroup),首先从JDK文档中对它进行一个大致的了解,如下: 下面开始用代码来进行说明,对于一个线程来说如果没有指定 ...
随机推荐
- Vs2019 C# .net core 将证书添加到受信任的根证书存储失败,出现以下错误:访问控制列表(ACL)结构无效
https://www.cnblogs.com/xiyuan/p/10632579.html 使用 vs2017 创建一个 ASP.NET Core Web 应用程序 -> Ctrl + F5 ...
- Oracle复习
这阵子忙着面试 ,之前搞项目一直用的 mysql ,然后面试的大公司!(呵呵)偏偏用的是 oracle 没办法恶补 前面学过的但是没怎么用的Oracle 基础知识,打算主要从下面几点入手. 环境搭建: ...
- 解决sqoop抽数报错:IO Error: Connection reset
遇到的问题:进行sqoop抽数时,虽然能成功执行,但是过程中有很多这样的信息 19/11/20 15:17:11 INFO mapreduce.Job: Task Id : attempt_15737 ...
- 前阿里P8架构师谈如何设计优秀的API
随着大数据.公共平台等互联网技术的日益成熟,API接口的重要性日益凸显,从公司的角度来看,API可以算作是公司一笔巨大的资产,公共API可以捕获用户.为公司做出许多贡献.对于个人来说,只要你编程,你就 ...
- 用cef Python打造自己的浏览器
背景 项目需要做一个客户端的壳,内置浏览器,访问指定 的url 采用技术 python3.5 cefpython https://github.com/cztomczak/cefpython#inst ...
- 如何利用while语句根据用户输入要求打印菱形图案
需求:如何利用while语句根据用户输入要求打印菱形图案 diamond.py代码如下: x=int(input('Please input number: ')) i=1 j=1 while i&l ...
- Mybatis报错: There is no getter for property named xxx
在mapper文件中函数的形参上加上注解. 例如: 出现了如下错误:核心错误提示就是There is no getter for property named xxx ### Error qu ...
- C#自由组合本地缓存、分布式缓存和数据库的数据
一.背景介绍: 我们在进行数据存储的时候,有时候会加入本地缓存.分布式缓存以及数据库存储三级的结构,当我们取值的时候经常是像下面这样的流程: 1.先取本地缓存,如果值存在直接返回 2.本地缓存不存在, ...
- Disable trigger to avoid the ID is auto-updated
CREATE TABLE COBRA.COBRA_PRODUCT_INFO_BAK AS SELECT * FROM COBRA.COBRA_PRODUCT_INFO; TRUNCATE TABLE ...
- 【万能的DFS和BFS基础框架】-多刷题才是硬道理!