日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java线程组(ThreadGroup)使用

發(fā)布時間:2025/4/9 java 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java线程组(ThreadGroup)使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JDK 對線程組類注釋:

A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.
A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups.

可以把線程歸屬到某一個線程組中,線程組中可以有線程對象,也可以有線程組,組中還可以有線程,這樣的組織結(jié)構(gòu)有點類似于樹的形式,如圖所示.

線程組的作用是:可以批量管理線程或線程組對象,有效地對線程或線程組對象進行組織

?

1.線程組示例:

展示線程組結(jié)構(gòu)代碼實例如下:

/*** 類功能描述:** @author WangXueXing create at 18-12-27 下午3:25* @version 1.0.0*/ public class ThreadGroupTest implements Runnable {@Overridepublic void run() {try {while (!Thread.currentThread().isInterrupted()) {Thread currentThread = Thread.currentThread();System.out.println("current thread:" + currentThread.getName()+" thread group:"+currentThread.getThreadGroup().getName()+" parent thread group:"+currentThread.getThreadGroup().getParent().getName());Thread.sleep(3000);}} catch (InterruptedException e) {e.printStackTrace();}}public static void main(String[] args) {ThreadGroup rootThreadGroup = new ThreadGroup("root線程組1");Thread t0 = new Thread(rootThreadGroup, new ThreadGroupTest(), "Thread 0");t0.start();ThreadGroup tg = new ThreadGroup(rootThreadGroup,"線程組1");ThreadGroup tg2 = new ThreadGroup(rootThreadGroup,"線程組2");Thread t1 = new Thread(tg, new ThreadGroupTest(), "Thread 1");Thread t2 = new Thread(tg, new ThreadGroupTest(), "Thread 2");t1.start();t2.start();Thread t3 = new Thread(tg2, new ThreadGroupTest(), "Thread 3");Thread t4 = new Thread(tg2, new ThreadGroupTest(), "Thread 4");t3.start();t4.start();} }

  打印結(jié)果如下:

current thread:Thread 0 thread group:root線程組1 parent thread group:main current thread:Thread 2 thread group:線程組1 parent thread group:root線程組1 current thread:Thread 1 thread group:線程組1 parent thread group:root線程組1 current thread:Thread 3 thread group:線程組2 parent thread group:root線程組1 current thread:Thread 4 thread group:線程組2 parent thread group:root線程組1 current thread:Thread 1 thread group:線程組1 parent thread group:root線程組1 current thread:Thread 0 thread group:root線程組1 parent thread group:main current thread:Thread 2 thread group:線程組1 parent thread group:root線程組1 current thread:Thread 3 thread group:線程組2 parent thread group:root線程組1 ......

  

?

2.線程組項目中應用(線程組內(nèi)的線程異常統(tǒng)一管理):

最近有個需求,生成復雜報表-從很多表數(shù)據(jù)中分析統(tǒng)計到一個報表。

實現(xiàn)思路:

多個線程分別到不同表中查數(shù)據(jù)并統(tǒng)計分析,任何一個線程失敗整體報表生成失敗,記錄日志并立即中斷其他線程。全部線程成功,報表生成成功。

廢話少說以下利用Java線程組結(jié)合CountDownLatch實現(xiàn)代碼如下:

/*** 多線程獲取報表數(shù)據(jù)* @param reportId 報表ID* @return*/ def getReportData(reportId: Long, supplierDetailMap: ConcurrentHashMap[Integer, Supplier]):ConcurrentHashMap[Integer, AnyRef] = {val dataMap = new ConcurrentHashMap[Integer, AnyRef]()//多線程同步器val conutDownLatch = new CountDownLatch(3)//實例化線程組val genThreadGroup = new GenThreadGroup(request.reportInfo.reportType.name, reportId)//獲取當月已開返利new Thread(genThreadGroup, new Runnable {override def run(): Unit = {dataMap.put(OPENED_REBATE_CODE, SupplierAccountDetailQuerySql.getTaxDiscountByMonth(request))conutDownLatch.countDown()}}, "獲取當月已開返利").start()//獲取當月累計解押款new Thread(genThreadGroup, new Runnable {override def run(): Unit = {dataMap.put(DEPOSIT_BY_MONTH, SupplierAccountDetailQuerySql.getDepositAmountByMonth(request))conutDownLatch.countDown()}}, "獲取當月累計解押款").start()//結(jié)算單的含稅金額new Thread(genThreadGroup, new Runnable {override def run(): Unit = {dataMap.put(ACCOUNT_PAYABLE_AMOUNT, SupplierAccountDetailQuerySql.getAccountPayableAmount(request))conutDownLatch.countDown()}}, "獲取結(jié)算單的含稅金額").start()//所有線程都執(zhí)行完成conutDownLatch.await()dataMap }

  

統(tǒng)一捕獲異常的線程組定義如下: /*** 定義報表生成線程組** @author BarryWang create at 2018/5/21 11:04* @version 0.0.1*/ class GenThreadGroup(groupName: String, reportId: Long) extends ThreadGroup(groupName){val logger: Logger = LoggerFactory.getLogger(classOf[GenThreadGroup])/*** 定義線程組中任意一個線程異常處理* @param thread 當前線程* @param exception 異常*/override def uncaughtException(thread: Thread, exception: Throwable): Unit = {logger.error(s"報表(ID:${reportId})生成異常, 線程組:${groupName}; 線程:${thread.getName} 失敗", exception)thread.getThreadGroup.interrupt()} }

  

轉(zhuǎn)載于:https://www.cnblogs.com/barrywxx/p/9976417.html

總結(jié)

以上是生活随笔為你收集整理的Java线程组(ThreadGroup)使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。