当前位置:网站首页>线程组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
边栏推荐
猜你喜欢

Gartner预测云迁移规模大幅增长;云迁移的优势是什么?

Call wechat customer service applet

政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
poi操作word模板替换数据并且导出word

Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in

Recyclerview advanced use (I) - simple implementation of sideslip deletion

连接公司跳板机取别名

MySQL数据库讲解(十)

RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复

redis数据库讲解二(redis高可用、持久化、性能管理)
随机推荐
不同时间类型的执行计划计算
关于密匙传递的安全性和数字签名
RecyclerView高级使用(二)-垂直拖拽排序的简单实现
Detailed tutorial on the use of setinterval timing function of wechat applet
帆软之单元格部分字体变颜色
Chrome插件 之 Selenium IDE、XPath 安装
HyperMotion云迁移助力中国联通,青云完成某央企上云项目,加速该集团核心业务系统上云进程
某政务云项目业务系统迁移调研实践
Homebrew是什么?以及使用
封装logging模块
jacob打印word
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
如何快速批量创建文本文档?
VMware installation 64 bit XP Chinese tutorial
云容灾是什么意思?云容灾和传统容灾的区别?
百度图片识别自定义实现(替代AipOcr)
帆软报表设置单元格填报以及根据值的大小进行排名方法
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
Logging module
Mock测试