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. Ifrecurse
istrue
, 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
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
- 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文档中对它进行一个大致的了解,如下: 下面开始用代码来进行说明,对于一个线程来说如果没有指定 ...
随机推荐
- (六)Spring Boot之日志配置-logback和log4j2
一.简介 支持日志框架:Java Util Logging, Log4J2 and Logback,默认是使用logback 配置方式: 默认配置文件配置 引用外部配置文件配置 二.默认配置文件配置( ...
- 数据库与MySQL进阶(4)
1,事务 事务指的是满足 ACID 特性的一组操作,可以通过 Commit 提交一个事务,也可以使用 Rollback 进行回滚. 1.1 ACID四大特性 原子性(Atomicity) 事务被视为不 ...
- $.ajax()属性详解
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...
- 微信小程序 上传图片并等比列压缩到指定大小
微信小程序官方API中 wx.chooseImage() 是可以进行图片压缩的,可惜的是不能压缩到指定大小. 实际开发中需求可能是压缩到指定大小: 原生js可以使用canvas来压缩,但由于微信小程 ...
- Java 之 可变参数
可变参数 在JDK1.5之后,如果我们定义一个方法需要接受多个参数,并且多个参数类型一致,我们可以对其简化成如下格式: 修饰符 返回值类型 方法名(参数类型... 形参名){ } 其实这个书写完全等价 ...
- 卷积神经网络(CNN)的训练过程
卷积神经网络的训练过程 卷积神经网络的训练过程分为两个阶段.第一个阶段是数据由低层次向高层次传播的阶段,即前向传播阶段.另外一个阶段是,当前向传播得出的结果与预期不相符时,将误差从高层次向底层次进行传 ...
- 【Redis】分布式Session
一.问题引出 1.1 Session的原理 1.2 问题概述 二.解决方案 三.代码实现-使用Token代替Session 3.1 Service 3.2 TokenController 一.问题引出 ...
- What Linux bind mounts are really doing
Lots of Unixes have some form of 'loopback' mounts, where you can mount a bit of an existing filesys ...
- nmap中文帮助文档
简介: Nmap(“ Network Mapper ”)是用于网络探索和安全审核的开源工具.它设计用于快速扫描大型网络,尽管它可以在单个主机上正常运行.Nmap以新颖的方式使用原始IP数据包来确定网络 ...
- C#当中的BeginInvoke和EndInvoke
我们已经知道 C#当中 存在async/await .BackGroudWorker类以及TPL(任务并行库).当然C#还有一些旧的模式来支持异步编程.参考<C#图解教程> 1. Beg ...