当前位置:网站首页>线程组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
边栏推荐
- 政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
- Check in system based on ibeacons
- JDBC详解
- VMWare安装64位XP中文教程
- 关于云容灾,你需要知道这些
- 什么是云迁移?云迁移的四种模式分别是?
- Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
- On September 8, the night before going to Songshan Lake
- 基于CM管理的CDH6.3.2集群集成Atlas2.1.0
- Homebrew是什么?以及使用
猜你喜欢

PyMySQL

星界边境文本自动翻译机(高级版)使用说明

按实际取,每三级分类汇总一次,看图知需求

VMware Workstation 无法连接到虚拟机。系统找不到指定的文件

在MAC上安装mysql

There is a mining virus in the server

Chrome插件 之 Selenium IDE、XPath 安装

Intégration de Clusters CDH Phoenix basée sur la gestion cm

Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)

利用json-server在本地创建服务器请求
随机推荐
帆软调用动态传参的方法,在标题中设置参数
某政务云项目业务系统迁移调研实践
教育行业云迁移最佳实践:海云捷迅使用HyperMotion云迁移产品为北京某大学实施渐进式迁移,成功率100%
Homebrew是什么?以及使用
Essential difference between restful WebService and gSOAP webservice
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
VMware15Pro在Deepin系统里面挂载真机电脑硬盘
修改ddt生成的测试用例名称
预览CSV文件
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
VMware 15pro mounts the hard disk of the real computer in the deepin system
不同时间类型的执行计划计算
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
Intégration de Clusters CDH Phoenix basée sur la gestion cm
MySQL数据库讲解(八)
使用Postman进行Mock测试
PySide2
Jacob print word
Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)