当前位置:网站首页>线程组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
边栏推荐
猜你喜欢
Research on recyclerview details - Discussion and repair of recyclerview click dislocation
Postman的安装使用及填坑心得
Some good articles on pthread multithreading
Visio installation error 1:1935 2: {XXXXXXXX
利用json-server在本地创建服务器请求
VMware installation 64 bit XP Chinese tutorial
PySide2
使用Postman进行Mock测试
How QT designer adds resource files
sql中出现一个变态问题
随机推荐
Recyclerview advanced use (I) - simple implementation of sideslip deletion
postman批量生产body信息(实现批量修改数据)
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
OpenStack命令操作
VMware15Pro在Deepin系统里面挂载真机电脑硬盘
按实际取,每三级分类汇总一次,看图知需求
FBS (fman build system) packaging
VMware 15pro mounts the hard disk of the real computer in the deepin system
微信小程序的订阅号开发(消息推送)
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
sql中出现一个变态问题
MySQL数据库讲解(七)
Some good articles on pthread multithreading
容灾有疑问?点这里
Essential difference between restful WebService and gSOAP webservice
修改ddt生成的测试用例名称
Jacob print word
POI operation word template replaces data and exports word
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化