Java并发编程之ThreadGroup

ThreadGroup是Java提供的一种对线程进行分组管理的手段,可以对所有线程以组为单位进行操作,如设置优先级、守护线程等。

线程组也有父子的概念,如下图:

线程组的创建

 1 public class ThreadGroupCreator {
 2 
 3     public static void main(String[] args) {
 4         //获取当前线程的group
 5         ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
 6         //在当前线程执行流中新建一个Group1
 7         ThreadGroup group1 = new ThreadGroup("Group1");
 8         //Group1的父线程,就是main线程所在Group
 9         System.out.println(group1.getParent() == currentGroup);
10         //定义Group2, 指定group1为其父线程
11         ThreadGroup group2 = new ThreadGroup(group1, "Group2");
12         System.out.println(group2.getParent() == group1);
13     }
14 }

线程组的基本操作

注意:后添加进线程组的线程,其优先级不能大于线程组的优先级

 1 public class ThreadGroupBasic {
 2 
 3     public static void main(String[] args) throws InterruptedException {
 4         
 5         ThreadGroup group = new ThreadGroup("group1");
 6         Thread thread = new Thread(group, () -> {
 7             while(true) {
 8                 try {
 9                     TimeUnit.SECONDS.sleep(1);
10                 } catch (InterruptedException e) {
11                     e.printStackTrace();
12                 }
13             }
14         }, "thread");
15         thread.setDaemon(true);
16         thread.start();
17         
18         TimeUnit.MILLISECONDS.sleep(1);
19         
20         ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
21         //递归获取mainGroup中活跃线程的估计值
22         System.out.println("activeCount = " + mainGroup.activeCount());
23         //递归获mainGroup中的活跃子group
24         System.out.println("activeGroupCount = " + mainGroup.activeGroupCount());
25         //获取group的优先级, 默认为10
26         System.out.println("getMaxPriority = " + mainGroup.getMaxPriority());
27         //获取group的名字
28         System.out.println("getName = " + mainGroup.getName());
29         //获取group的父group, 如不存在则返回null
30         System.out.println("getParent = " + mainGroup.getParent());
31         //活跃线程信息全部输出到控制台
32         mainGroup.list();
33         System.out.println("----------------------------");
34         //判断当前group是不是给定group的父线程, 如果两者一样,也会返回true
35         System.out.println("parentOf = " + mainGroup.parentOf(group));
36         System.out.println("parentOf = " + mainGroup.parentOf(mainGroup));
37 
38     }
39 
40 }

线程组的Interrupt

 1 ublic class ThreadGroupInterrupt {
 2 
 3     public static void main(String[] args) throws InterruptedException {
 4         ThreadGroup group = new ThreadGroup("TestGroup");
 5         new Thread(group, () -> {
 6             while(true) {
 7                 try {
 8                     TimeUnit.MILLISECONDS.sleep(2);
 9                 } catch (InterruptedException e) {
10                     //received interrupt signal and clear quickly
11                     System.out.println(Thread.currentThread().isInterrupted());
12                     break;
13                 }
14             }
15             System.out.println("t1 will exit");
16         }, "t1").start();
17         new Thread(group, () -> {
18             while(true) {
19                 try {
20                     TimeUnit.MILLISECONDS.sleep(2);
21                 } catch (InterruptedException e) {
22                     //received interrupt signal and clear quickly
23                     System.out.println(Thread.currentThread().isInterrupted());
24                     break;
25                 }
26             }
27             System.out.println("t2 will exit");
28         }, "t2").start();
29         //make sure all threads start
30         TimeUnit.MILLISECONDS.sleep(2);
31         
32         group.interrupt();
33     }
34 
35 }

线程组的destroy

 1 public class ThreadGroupDestroy {
 2 
 3     public static void main(String[] args) {
 4         ThreadGroup group = new ThreadGroup("TestGroup");
 5         ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
 6         //before destroy
 7         System.out.println("group.isDestroyed=" + group.isDestroyed());
 8         mainGroup.list();
 9         
10         group.destroy();
11         //after destroy
12         System.out.println("group.isDestroyed=" + group.isDestroyed());
13         mainGroup.list();
14     }
15 
16 }

线程组设置守护线程组

线程组设置为守护线程组,并不会影响其线程是否为守护线程,仅仅表示当它内部没有active的线程的时候,会自动destroy

 

 1 public class ThreadGroupDaemon {
 2 
 3     public static void main(String[] args) throws InterruptedException {
 4         ThreadGroup group1 = new ThreadGroup("group1");
 5         new Thread(group1, () -> {
 6             try {
 7                 TimeUnit.SECONDS.sleep(1);
 8             } catch (InterruptedException e) {
 9                 e.printStackTrace();
10             }
11         }, "group1-thread1").start();
12         ThreadGroup group2 = new ThreadGroup("group2");
13         new Thread(group2, () -> {
14             try {
15                 TimeUnit.SECONDS.sleep(1);
16             } catch (InterruptedException e) {
17                 e.printStackTrace();
18             }
19         }, "group1-thread2").start();
20         group2.setDaemon(true);
21         
22         TimeUnit.SECONDS.sleep(3);
23         System.out.println(group1.isDestroyed());
24         System.out.println(group2.isDestroyed());
25     }
26 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/450276.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springboot 缓存ehcache的简单使用

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 步骤&#xff1a; 1. pom文件中加 maven jar包&#xff1a; <!-- ehcache 缓存 --><dependency><groupId>net.sf.eh…

Spring boot + mybatis plus 快速构建项目,生成基本业务操作代码。

---进行业务建表&#xff0c;这边根据个人业务分析&#xff0c;不具体操作 --加入mybatis plus pom依赖 <!-- mybatis-plus 3.0.5--> <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId>&l…

给手机浏览器减负 轻装上阵才能速度制胜

随着手机浏览器的发展&#xff0c;浏览器已经变得臃肿不堪&#xff0c;各种“功能”系于一身&#xff0c;有广告、社区、乐园等等&#xff0c;我们真的需要它们吗&#xff1f;如何才能让浏览器做到轻装上阵&#xff0c;又能高效满足我们需求呢&#xff1f; 过多“功能”的浏览器…

653. Two Sum IV - Input is a BST

题目来源&#xff1a; 自我感觉难度/真实难度&#xff1a; 题意&#xff1a; 分析&#xff1a; 自己的代码&#xff1a; class Solution(object):def findTarget(self, root, k):""":type root: TreeNode:type k: int:rtype: bool"""Allself.InO…

解决 dubbo问题:Forbid consumer 192.xx.xx.1 access service com.xx.xx.xx.rpc.api.xx from registry 116.xx1

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 我的情况是&#xff1a; 原本我把服务放在A工程中&#xff0c;后来改到B工程中了&#xff0c;所以原来的服务不存在了&#xff0c;查不…

vue学习:7、路由跳转

2019独角兽企业重金招聘Python工程师标准>>> <body><div id"app"></div></body><script type"text/javascript">var Login {template: <div>我是登陆界面</div>};var Register {template: <div…

Spring Retry 重试机制实现及原理

概要 Spring实现了一套重试机制&#xff0c;功能简单实用。Spring Retry是从Spring Batch独立出来的一个功能&#xff0c;已经广泛应用于Spring Batch,Spring Integration, Spring for Apache Hadoop等Spring项目。本文将讲述如何使用Spring Retry及其实现原理。 背景 重试&…

inline 内联函数详解 内联函数与宏定义的区别

一、在C&C中   一、inline 关键字用来定义一个类的内联函数&#xff0c;引入它的主要原因是用它替代C中表达式形式的宏定义。表达式形式的宏定义一例&#xff1a;#define ExpressionName(Var1,Var2) ((Var1)(Var2))*((Var1)-(Var2))为什么要取代这种形式呢&#xff0c;且…

Oracle序列更新为主键最大值

我们在使用 Oracle 数据库的时候&#xff0c;有时候会选择使用自增序列作为主键。但是在开发过程中往往会遇到一些不规范的操作&#xff0c;导致表的主键值不是使用序列插入的。这样在数据移植的时候就会出现各种各样的问题。当然数据库主键不使用序列是一种很好的方式&#xf…

dubbo forbid service的解决办法

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 017-05-31 10:36:54.523 [http-nio-8080-exec-5] ERROR c.h.pdl.web.APIExceptionHandler - Unknown Exception, URI /payday-loan-co…

用SSH登录远程的机器,在远程机器上执行本地机器上的脚本

假设本地的机器IP为10.245.111.90&#xff0c;我们想要在10.245.111.93上执行一个保存在10.245.111.90上的脚本。经过测试通过的命令如下&#xff1a;ssh root10.245.111.93 bash -s < /root/testlocal.sh如果要带参数的话&#xff0c;那就需要参考这篇文章中描述的代码了。…

golang学习之旅(1)

这段时间我开始了golang语言学习&#xff0c;其实也是为了个人的职业发展的拓展和衍生&#xff0c;语言只是工具&#xff0c;但是每个语言由于各自的特点和优势&#xff0c;golang对于当前编程语言的环境&#xff0c;是相对比较新的语言&#xff0c;对于区块链&#xff0c;大数…

为什么要在Linux平台上学C语言?用Windows学C语言不好吗?

用Windows还真的是学不好C语言。C语言是一种面向底层的编程语言&#xff0c;要写好C程序&#xff0c;必须对操作系统的工作原理非常清楚&#xff0c;因为操作系统也是用C写的&#xff0c;我们用C写应用程序直接使用操作系统提供的接口&#xff0c;Linux是一种开源的操作系统&am…

数据库中Schema(模式)概念的理解

在学习SQL的过程中&#xff0c;会遇到一个让你迷糊的Schema的概念。实际上&#xff0c;schema就是数据库对象的集合&#xff0c;这个集合包含了各种对象如&#xff1a;表、视图、存储过程、索引等。为了区分不同的集合&#xff0c;就需要给不同的集合起不同的名字&#xff0c;默…

linux系统中打rz命令后出现waiting to receive.**B0100000023be50

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 linux系统中打rz命令后出现 waiting to receive.**B0100000023be50 而没有出现选择文件弹出框是什么问题&#xff1a; 我本来用的是 gi…

golang学习之旅(2)- go的数据基本数据类型及变量定义方式

叮铃铃&#xff0c;这不有人在评论问下一篇何时更新&#xff0c;这不就来了嘛&#xff0c;&#x1f604; 今天我们说说golang 的基本数据类型 基本类型如下&#xff1a; //基本类型 布尔类型&#xff1a;bool 即true 、flase 类似于java中的boolean 字符类型&#xff1a;s…

StackExchange.Redis 官方文档(六) PipelinesMultiplexers

流水线和复用 糟糕的时间浪费。现代的计算机以惊人的速度产生大量的数据&#xff0c;而且高速网络通道(通常在重要的服务器之间同时存在多个链路)提供了很高的带宽&#xff0c;但是计算机花费了大量的时间在 等待数据 上面&#xff0c;这也是造成使用持久性链接的编程方式越来越…

开发优秀产品的六大秘诀

摘要&#xff1a;本文是Totango的联合创始人兼公司CEO Guy Nirpaz发表在Mashable.com上的文章。无论是在哪个行业&#xff0c;用户永远是一款产品的中心&#xff0c;本文作者就以用户为中心&#xff0c;为大家讲述了六个如何为企业产品添加功能的秘诀。 随着云计算的发展&#…

Spring Boot下无法加载主类 org.apache.maven.wrapper.MavenWrapperMain问题解决

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 引言&#xff1a; 在SpringBoot中需要使用mvnw来做相关操作&#xff0c;但是却有时候会报出达不到MavenWrapperMain的错误信息&#xff…

【前端面试】字节跳动2019校招面经 - 前端开发岗(二)

【前端面试】字节跳动2019校招面经 - 前端开发岗&#xff08;二&#xff09; 因为之前的一篇篇幅有限&#xff0c;太长了看着也不舒服&#xff0c;所以还是另起一篇吧?一、 jQuery和Vue的区别 jQuery 轻量级Javascript库Vue 渐进式Javascript-MVVM框架jQuery和Vue的对比 jQuer…