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

倒计时1天~2022云容灾产品线上发布会即将开始

MySQL数据库讲解(九)

RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能

回顾2021:如何帮助客户扫清上云最后一公里的障碍?

HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化

redis数据库讲解二(redis高可用、持久化、性能管理)

Operation instructions of star boundary text automatic translator

Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)

正则表达式
Jira截取全图
随机推荐
1到100号的灯开关问题
帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
Logging module
Visio画拓扑图随记
帆软实现分页时第一行和最后两行冻结方式
使用Executors类快速创建线程池
How to do a project easily
OpenStack如何跨版本升级
MySQL lock database lock
mysql查询库大小
Understand the concepts of virtual base class, virtual function and pure virtual function (turn)
Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
剑指offer刷题(1)--面向华为
关于Jmeter启动闪退问题
多云数据流转?云上容灾?年前最后的价值内容分享
JDBC details
FBS (fman build system) packaging
帆软调用动态传参的方法,在标题中设置参数
关于训练过程中损失函数出现断崖式增长的问题
elk安装