当前位置:网站首页>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
边栏推荐
- FBS (fman build system) packaging
- HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
- jsp学习1
- Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)
- Homebrew是什么?以及使用
- 文字组合,不重复,做搜索或查询关键字匹配
- ActiveMQ Basics
- Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
- MySQL数据库讲解(十)
- Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
猜你喜欢
随机推荐
Pass in external parameters to the main function in clion
关于云容灾,你需要知道这些
返回数组排序后下标
Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
剑指offer刷题(1)--面向华为
拨开云雾synchronized使用五种方式介绍
RecyclerView高级使用(一)-侧滑删除的简单实现
报表FCRA考试题集及答案(错了11题)
MySQL数据库讲解(九)
OpenStack如何跨版本升级
教育行业云迁移最佳实践:海云捷迅使用HyperMotion云迁移产品为北京某大学实施渐进式迁移,成功率100%
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
星界边境文本自动翻译机使用说明
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
RecyclerView高级使用(二)-垂直拖拽排序的简单实现
yml引用其他变量
Jira截取全图
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
关于密匙传递的安全性和数字签名