当前位置:网站首页>Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
2022-04-23 14:14:00 【pureluckyfish】
One 、ThreadGroup Class introduction
A thread group is a A tree structure ,ThreadGroup Class has some methods to delete or add and maintain this tree , There are also some methods to query the node status and hierarchical relationship of the tree ; Thread group's Access control and linux The genera are similar to the main genus group : A thread is only allowed to access information about its own thread group , However, it is not allowed to access the information of the parent thread group of its thread group or any other thread group .
Code example :
package com.yu;
public class ThreadGroupTest {
public static void main(String[] args) throws Exception {
ThreadGroup tg_parent = new ThreadGroup(" The parent thread group ");
ThreadGroup tg_son = new ThreadGroup(tg_parent, " The child thread group ");
Thread t1 = new Thread(tg_son, () -> {
System.out.println(" Thread name :" + Thread.currentThread().getName());
System.out.println(" Thread group name :" + Thread.currentThread().getThreadGroup().getName());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t1 Threads ");
Thread t2 = new Thread(tg_parent, () -> {
System.out.println(" Thread name :" + Thread.currentThread().getName());
System.out.println(" Thread group name :" + Thread.currentThread().getThreadGroup().getName());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t2 Threads ");
Thread t3 = new Thread(tg_parent, () -> {
System.out.println(" Thread name :" + Thread.currentThread().getName());
System.out.println(" Thread group name :" + Thread.currentThread().getThreadGroup().getName());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t2 Threads ");
System.out.println(" Thread group name :"+t1.getThreadGroup().getName());
System.out.println(" Whether the thread is in a given thread group :"+t1.getThreadGroup().parentOf(tg_parent));
System.out.println(" Whether the thread is in a given thread group :"+t1.getThreadGroup().parentOf(tg_son));
System.out.println(" Number of live threads in thread group :"+tg_parent.activeCount());// Is an evaluation value
System.out.println(" Number of thread groups alive in thread group :"+tg_parent.activeGroupCount());// Is an evaluation value
System.out.println(" Maximum priority of thread group :"+t1.getThreadGroup().getMaxPriority());
}
}
Use scenarios : 1 Big task , It can be divided into multiple independent sub threads for concurrent processing , As long as one of the sub threads completes the task condition , Even if it's done , Call threadGroup.interrupt() Method Other sub threads can be stopped ;2 Big task , It can be divided into multiple independent sub threads for concurrent processing , Finally, wait for the execution of all sub threads to end, and then continue to execute .
Two 、ThreadFactory The interface is introduced
ThreadFactory It's an interface , only one Thread newThread(Runnable r) Method ;
The custom thread factory implementation class produces threads like a factory pipeline according to certain rules , These threads “ They all look alike ” ;
Custom thread factory implementation class , You can track when and how many threads are created by the thread pool , You can also customize the thread name , Group , priority , Even directly set all threads as daemon threads . You can customize the thread pool to set the status of all threads in the pool more freely .
Code example :
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 {
// Custom thread factory
ThreadFactory threadFactoryOwner = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("threadFactoryOwner-pool-" + t.getId());
t.setDaemon(true);// Set to daemons
return t;
}
};
// Create a thread pool using a custom factory
ExecutorService executorService = Executors.newFixedThreadPool(10, threadFactoryOwner);
for (int i = 0; i < 10; i++) {
executorService.submit(() -> {
System.out.println(" Thread name :" + Thread.currentThread().getName());
System.out.println(" Thread group :" + Thread.currentThread().getThreadGroup());
System.out.println(" Threads 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 The default thread factory implementation : When creating a thread pool, specify the thread factory implementation class , If not specified, use JDK Default thread factory .

3、 ... and 、 The connection between the two
When customizing the thread factory implementation class, you can use the specified thread group .
版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406486413.html
边栏推荐
- 帆软调用动态传参的方法,在标题中设置参数
- A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
- 星界边境文本自动翻译机使用说明
- 服务器日志分析工具(识别,提取,合并,统计异常信息)
- 星界边境Starbound创意工坊订阅的mod的存放路径
- gzip和gunzip 解压参数详解
- 帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
- Idea控制台乱码解决
- 百度笔试2022.4.12+编程题目:简单整数问题
- ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
猜你喜欢
VMware Workstation 无法连接到虚拟机。系统找不到指定的文件
Detailed tutorial on the use of setinterval timing function of wechat applet
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
使用DialogFragment的一些感受及防踩坑经验(getActivity、getDialog为空,cancelable无效等)
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
Postman的安装使用及填坑心得
什么是云迁移?云迁移的四种模式分别是?
Logback logger and root
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
帆软中需要设置合计值为0时,一整行都不显示的解决办法
随机推荐
文字组合,不重复,做搜索或查询关键字匹配
Logging module
dp-能量项链
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
json date时间日期格式化
DP energy Necklace
mysql 5.1升级到5.69
Pass in external parameters to the main function in clion
Can I compile the header file and source file of the template separately
openstack理论知识
postman批量生产body信息(实现批量修改数据)
Detailed tutorial on the use of setinterval timing function of wechat applet
报表FCRA考试题集及答案(错了11题)
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
jsp学习1
RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复
krpano全景之vtour文件夹和tour
Oracle-数据泵使用
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态