当前位置:网站首页>线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
2022-04-23 14:07:00 【pureluckyfish】
一、ThreadGroup类介绍
线程组是一个树形结构 ,ThreadGroup类有一些方法可以删除或者新增维护这棵树,也有一些查询树节点状态和层级关系的方法;线程组的权限控制和linux的属主属组类似:线程只允许访问关于它自己的线程组的信息,但是不允许访问它所在线程组的父线程组或任何其他线程组的信息。
代码示例:
package com.yu;
public class ThreadGroupTest {
public static void main(String[] args) throws Exception {
ThreadGroup tg_parent = new ThreadGroup("父线程组");
ThreadGroup tg_son = new ThreadGroup(tg_parent, "子线程组");
Thread t1 = new Thread(tg_son, () -> {
System.out.println("线程名称:" + Thread.currentThread().getName());
System.out.println("线程组名称:" + Thread.currentThread().getThreadGroup().getName());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t1线程");
Thread t2 = new Thread(tg_parent, () -> {
System.out.println("线程名称:" + Thread.currentThread().getName());
System.out.println("线程组名称:" + Thread.currentThread().getThreadGroup().getName());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t2线程");
Thread t3 = new Thread(tg_parent, () -> {
System.out.println("线程名称:" + Thread.currentThread().getName());
System.out.println("线程组名称:" + Thread.currentThread().getThreadGroup().getName());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t2线程");
System.out.println("线程组名称:"+t1.getThreadGroup().getName());
System.out.println("线程是否在给定的线程组中:"+t1.getThreadGroup().parentOf(tg_parent));
System.out.println("线程是否在给定的线程组中:"+t1.getThreadGroup().parentOf(tg_son));
System.out.println("线程组中活的线程数:"+tg_parent.activeCount());//是一个评估值
System.out.println("线程组中活的线程组数:"+tg_parent.activeGroupCount());//是一个评估值
System.out.println("线程组的最大优先级:"+t1.getThreadGroup().getMaxPriority());
}
}
使用场景: 1 大型任务,可分成多个独立的子线程并发进行,只要其中的某个子线程完成了任务条件,就算任务完成,则调用 threadGroup.interrupt() 方法 其他的子线程就可以停止了;2 大型任务,可分成多个独立的子线程并发进行,最后等待所有的子线程执行结束然后继续往下执行。
二、ThreadFactory接口介绍
ThreadFactory是一个接口,只有一个Thread newThread(Runnable r) 方法;
自定义线程工厂实现类按照一定的规则像工厂流水线那样生产线程,这些线程 “ 长得都挺像的 ” ;
自定义线程工厂实现类,可以跟踪线程池究竟在何时创建了多少线程,也可以自定义线程名称,组,优先级,甚至直接设定所有线程为守护线程。可以通过自定义线程池更加自由的设置池子里所有线程的状态。
代码示例:
package com.yu;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class ThreadFactoryTest {
public static void main(String[] args) throws Exception {
// 自定义线程工厂
ThreadFactory threadFactoryOwner = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("threadFactoryOwner-pool-" + t.getId());
t.setDaemon(true);// 设置为守护进程
return t;
}
};
// 使用自定义的工厂创建线程池
ExecutorService executorService = Executors.newFixedThreadPool(10, threadFactoryOwner);
for (int i = 0; i < 10; i++) {
executorService.submit(() -> {
System.out.println("线程名称:" + Thread.currentThread().getName());
System.out.println("线程线程组:" + Thread.currentThread().getThreadGroup());
System.out.println("线程ID:" + Thread.currentThread().getId());
System.out.println("==========================================");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
Thread.sleep(2000);
}
}
}
JDK默认的线程工厂实现类:创建线程池的时候指定线程工厂实现类,如果不指定则使用JDK默认的线程工厂。
三、二者的联系
自定义线程工厂实现类的时候可以使用指定的线程组。
版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sinat_33918956/article/details/118897255
边栏推荐
- jsp学习3
- Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)
- GFS分布式文件系统(理论)
- Logging module
- 预览CSV文件
- 查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
- Research on recyclerview details - Discussion and repair of recyclerview click dislocation
- RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
- MySQL数据库讲解(八)
- 按实际取,每三级分类汇总一次,看图知需求
猜你喜欢
随机推荐
多云数据流转?云上容灾?年前最后的价值内容分享
VMware15Pro在Deepin系统里面挂载真机电脑硬盘
PySide2
帆软报表设置单元格填报以及根据值的大小进行排名方法
Wechat applet obtains login user information, openid and access_ token
在MAC上安装mysql
logback-logger和root
OpenStack如何跨版本升级
CDH cluster integration Phoenix based on CM management
FBS (fman build system) packaging
金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
倒计时1天~2022云容灾产品线上发布会即将开始
正则表达式
Jmeter设置环境变量支持在任意终端目录输入jmeter直接启动
使用Postman进行Mock测试
Chapter I review of e-commerce spike products
服务器日志分析工具(识别,提取,合并,统计异常信息)
Wechat applet positioning and ranging through low-power Bluetooth device (2)
POI operation word template replaces data and exports word
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication