当前位置:网站首页>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
边栏推荐
猜你喜欢
Detailed tutorial on the use of setinterval timing function of wechat applet
星界边境文本自动翻译机(高级版)使用说明
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
HyperMotion云迁移助力中国联通,青云完成某央企上云项目,加速该集团核心业务系统上云进程
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
更改plsql工具栏的图标大小
金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
随机推荐
JSP学习2
How QT designer adds resource files
多云数据流转?云上容灾?年前最后的价值内容分享
krpano全景之vtour文件夹和tour
01-NIO基础之ByteBuffer和FileChannel
使用开源调研工具Prophet是一种什么体验?
RecyclerView高级使用(一)-侧滑删除的简单实现
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
VMWare安装64位XP中文教程
mysql 5.1升级到5.611
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
rsync+inotify远程同步
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
帆软调用动态传参的方法,在标题中设置参数
Multiple inheritance virtual base exercises
STD:: map and STD:: vector memory free
返回数组排序后下标
快速搞懂线程实现的三种方式
Detailed tutorial on the use of setinterval timing function of wechat applet