当前位置:网站首页>線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
2022-04-23 14:13: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://yzsam.com/2022/04/202204231406486413.html
边栏推荐
猜你喜欢
随机推荐
Logging module
如何快速批量创建文本文档?
How QT designer adds resource files
MySQL lock database lock
About the configuration and use of json5 in nodejs
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
第四届“传智杯”全国大学生IT技能大赛(决赛B组) 题解
Some good articles on pthread multithreading
PySide2
星界边境文本自动翻译机使用说明
GFS分布式文件系统(理论)
Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
redis数据库讲解(三)redis数据类型
帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
leetcode--977. Squares of a Sorted Array
帆软中单元格中隔行变色以及数量大于100字体变大变红设置









