Java线程组(ThreadGroup)使用
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.
可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线程,这样的组织结构有点类似于树的形式,如图所示.
线程组的作用是:可以批量管理线程或线程组对象,有效地对线程或线程组对象进行组织
1.线程组示例:
展示线程组结构代码实例如下:
/*** 类功能描述:** @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();}
}
打印结果如下:
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.线程组项目中应用(线程组内的线程异常统一管理):
最近有个需求,生成复杂报表-从很多表数据中分析统计到一个报表。
实现思路:
多个线程分别到不同表中查数据并统计分析,任何一个线程失败整体报表生成失败,记录日志并立即中断其他线程。全部线程成功,报表生成成功。
废话少说以下利用Java线程组结合CountDownLatch实现代码如下:
/*** 多线程获取报表数据* @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()//结算单的含税金额new Thread(genThreadGroup, new Runnable {override def run(): Unit = {dataMap.put(ACCOUNT_PAYABLE_AMOUNT, SupplierAccountDetailQuerySql.getAccountPayableAmount(request))conutDownLatch.countDown()}}, "获取结算单的含税金额").start()//所有线程都执行完成conutDownLatch.await()dataMap
}
统一捕获异常的线程组定义如下:
/*** 定义报表生成线程组** @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()}
}
转载于:https://www.cnblogs.com/barrywxx/p/9976417.html
Java线程组(ThreadGroup)使用相关推荐
- 【java】java 线程组 ThreadGroup
1.概述 转载:线程组是什么?线程优先级如何设置? 2. 线程组(ThreadGroup) Java中用ThreadGroup来表示线程组,我们可以使用线程组对线程进行批量控制. ThreadGrou ...
- java 线程组作用_浅析Java中线程组(ThreadGroup类)
一.概念 Java中使用ThreadGroup类来代表线程组,表示一组线程的集合,可以对一批线程和线程组进行管理.可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线 ...
- java 线程组作用_Java线程组(ThreadGroup)使用
JDK 对线程组类注释: A thread group represents a set of threads. In addition, a thread group can also includ ...
- java 线程组 历遍_Java并发之线程组ThreadGroup介绍
线程组介绍 线程组(ThreadGroup)简单来说就是一个线程集合.线程组的出现是为了更方便地管理线程. 线程组是父子结构的,一个线程组可以集成其他线程组,同时也可以拥有其他子线程组.从结构上看,线 ...
- 线程组ThreadGroup分析详解 多线程中篇(三)
线程组,顾名思义,就是线程的组,逻辑类似项目组,用于管理项目成员,线程组就是用来管理线程. 每个线程都会有一个线程组,如果没有设置将会有些默认的初始化设置 而在java中线程组则是使用类ThreadG ...
- java线程组 线程池_JAVA多线程(三)-----线程组、线程池和线程相关类
一.线程组和未处理的异常 Thread类提供了如下几个构造器来设置新创建的线程属于哪个线程组: Thread(ThreadGroup group,Runnable target):以target的ru ...
- java group类_浅析Java中线程组(ThreadGroup类)
Java中使用ThreadGroup类来代表线程组,表示一组线程的集合,可以对一批线程和线程组进行管理.可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线程,这样的 ...
- java 线程组和线程_Java多线程 线程组原理及实例详解
线程组 线程组可以批量管理线程和线程组对象. 一级关联 例子如下,建立一级关联. public class MyThread43 implements Runnable{ public void ru ...
- java定时器阻塞主线程_Java基础_死锁、线程组、定时器Timer
一.死锁问题: 死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 比如,线程一需要第一把所,此时锁处于空闲状态,给了 ...
最新文章
- 实验四:汇编代码调用系统调用的工作过程
- 新手如何学drupal?
- python 面向对象(三)多继承
- localStorage的过期时间设置的方法?
- 【ARM】异常产生指令
- 文献记录(part66)--一种基于交叉熵的社区发现算法
- CentOS查看每个进程的网络流量
- 昨天事情还是比较多,让我晚上加了会班
- mysql无法与外部健形成约束_MySQL Rails:错误:150“外键约束不正确”
- Dubbo消费者代理的创建
- phaser java_Java 7的并发编程-Phaser
- STM32 ARM调试问题总结
- hp服务器风扇告警信息,HP ProLiant 服务器 - POST 错误消息和蜂鸣代码
- google hack 语法(渗透测试google黑客语法)
- Ubuntu16 网卡rtl8723be 驱动安装
- AWS 中文入门开发教学 28- 链接Rout53和freenom - 设置托管区(Hosted Zone)和名字服务器(NS)
- 【科研】博士学位论文评阅书
- 像素鸟 app 的设计与实现
- 分享给妈咪们减肥小感悟
- baked lighting
热门文章
- cmd中添加中文字体
- Building an MFC project for a non-Unicode character set is deprecated
- 使用匿名函数和内嵌函数处理多变量传递问题
- 【解决方案】Expected object of type torch.FloatTensor but found type torch.DoubleTensor
- 关于数学里的一些知识
- php ci ajax用户登录,使用jQuery和CI显示AJAX调用数据库的数据
- 第二十二讲 延迟定理
- 第二十一讲 卷积公式
- LCD编程_LCD控制器
- git仓库如果是私密的,每台电脑上导下来都需要进行ssh授权,所以一个项目不知一个ssh权限...