java高并发之线程组详解

作者:路人甲Java 时间:2023-02-15 21:30:25 

线程组

我们可以把线程归属到某个线程组中,线程组可以包含多个线程以及线程组,线程和线程组组成了父子关系,是个树形结构,如下图:

java高并发之线程组详解

使用线程组可以方便管理线程,线程组提供了一些方法方便方便我们管理线程。

创建线程关联线程组

创建线程的时候,可以给线程指定一个线程组,代码如下:


package com.itsoku.chat02;
import java.util.concurrent.TimeUnit;
/**
* <b>description</b>:<br>
* <b>time</b>:2019/7/13 17:53 <br>
* <b>author</b>:微信公众号:路人甲Java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!
*/
public class Demo1 {
   public static class R1 implements Runnable {
       @Override
       public void run() {
           System.out.println("threadName:" + Thread.currentThread().getName());
           try {
               TimeUnit.SECONDS.sleep(3);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
   }
   public static void main(String[] args) throws InterruptedException {
       ThreadGroup threadGroup = new ThreadGroup("thread-group-1");
       Thread t1 = new Thread(threadGroup, new R1(), "t1");
       Thread t2 = new Thread(threadGroup, new R1(), "t2");
       t1.start();
       t2.start();
       TimeUnit.SECONDS.sleep(1);
       System.out.println("活动线程数:" + threadGroup.activeCount());
       System.out.println("活动线程组:" + threadGroup.activeGroupCount());
       System.out.println("线程组名称:" + threadGroup.getName());
   }
}

输出结果:

threadName:t1
threadName:t2
活动线程数:2
活动线程组:0
线程组名称:thread-group-1

activeCount()方法可以返回线程组中的所有活动线程数,包含下面的所有子孙节点的线程,由于线程组中的线程是动态变化的,这个值只能是一个估算值。

为线程组指定父线程组

创建线程组的时候,可以给其指定一个父线程组,也可以不指定,如果不指定父线程组,则父线程组为当前线程的线程组,java api有2个常用的构造方法用来创建线程组:


public ThreadGroup(String name)
public ThreadGroup(ThreadGroup parent, String name)

第一个构造方法未指定父线程组,看一下内部的实现:


public ThreadGroup(String name) {
       this(Thread.currentThread().getThreadGroup(), name);
   }

系统自动获取当前线程的线程组作为默认父线程组。

上一段示例代码:


package com.itsoku.chat02;
import java.util.concurrent.TimeUnit;
/**
* <b>description</b>:<br>
* <b>time</b>:2019/7/13 17:53 <br>
* <b>author</b>:微信公众号:路人甲Java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!
*/
public class Demo2 {
   public static class R1 implements Runnable {
       @Override
       public void run() {
           Thread thread = Thread.currentThread();
           System.out.println("所属线程组:" + thread.getThreadGroup().getName() + ",线程名称:" + thread.getName());
           try {
               TimeUnit.SECONDS.sleep(3);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
   }
   public static void main(String[] args) throws InterruptedException {
       ThreadGroup threadGroup1 = new ThreadGroup("thread-group-1");
       Thread t1 = new Thread(threadGroup1, new R1(), "t1");
       Thread t2 = new Thread(threadGroup1, new R1(), "t2");
       t1.start();
       t2.start();
       TimeUnit.SECONDS.sleep(1);
       System.out.println("threadGroup1活动线程数:" + threadGroup1.activeCount());
       System.out.println("threadGroup1活动线程组:" + threadGroup1.activeGroupCount());
       System.out.println("threadGroup1线程组名称:" + threadGroup1.getName());
       System.out.println("threadGroup1父线程组名称:" + threadGroup1.getParent().getName());
       System.out.println("----------------------");
       ThreadGroup threadGroup2 = new ThreadGroup(threadGroup1, "thread-group-2");
       Thread t3 = new Thread(threadGroup2, new R1(), "t3");
       Thread t4 = new Thread(threadGroup2, new R1(), "t4");
       t3.start();
       t4.start();
       TimeUnit.SECONDS.sleep(1);
       System.out.println("threadGroup2活动线程数:" + threadGroup2.activeCount());
       System.out.println("threadGroup2活动线程组:" + threadGroup2.activeGroupCount());
       System.out.println("threadGroup2线程组名称:" + threadGroup2.getName());
       System.out.println("threadGroup2父线程组名称:" + threadGroup2.getParent().getName());
       System.out.println("----------------------");
       System.out.println("threadGroup1活动线程数:" + threadGroup1.activeCount());
       System.out.println("threadGroup1活动线程组:" + threadGroup1.activeGroupCount());
       System.out.println("----------------------");
       threadGroup1.list();
   }
}

输出结果:

所属线程组:thread-group-1,线程名称:t1
所属线程组:thread-group-1,线程名称:t2
threadGroup1活动线程数:2
threadGroup1活动线程组:0
threadGroup1线程组名称:thread-group-1
threadGroup1父线程组名称:main
----------------------
所属线程组:thread-group-2,线程名称:t4
所属线程组:thread-group-2,线程名称:t3
threadGroup2活动线程数:2
threadGroup2活动线程组:0
threadGroup2线程组名称:thread-group-2
threadGroup2父线程组名称:thread-group-1
----------------------
threadGroup1活动线程数:4
threadGroup1活动线程组:1
----------------------
java.lang.ThreadGroup[name=thread-group-1,maxpri=10]
Thread[t1,5,thread-group-1]
Thread[t2,5,thread-group-1]
java.lang.ThreadGroup[name=thread-group-2,maxpri=10]
Thread[t3,5,thread-group-2]
Thread[t4,5,thread-group-2]

代码解释:

1.threadGroup1未指定父线程组,系统获取了主线程的线程组作为threadGroup1的父线程组,输出结果中是:main

2.threadGroup1为threadGroup2的父线程组

3.threadGroup1活动线程数为4,包含了threadGroup1线程组中的t1、t2,以及子线程组threadGroup2中的t3、t4

4.线程组的list()方法,将线程组中的所有子孙节点信息输出到控制台,用于调试使用

根线程组

获取根线程组


package com.itsoku.chat02;
/**
* <b>description</b>:<br>
* <b>time</b>:2019/7/13 17:53 <br>
* <b>author</b>:微信公众号:路人甲Java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!
*/
public class Demo3 {
   public static void main(String[] args) {
       System.out.println(Thread.currentThread());
       System.out.println(Thread.currentThread().getThreadGroup());
       System.out.println(Thread.currentThread().getThreadGroup().getParent());
       System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent());
   }
}

运行上面代码,输出:

Thread[main,5,main]
java.lang.ThreadGroup[name=main,maxpri=10]
java.lang.ThreadGroup[name=system,maxpri=10]
null

从上面代码可以看出:

1.主线程的线程组为main

2.根线程组为system

看一下ThreadGroup的源码:


private ThreadGroup() {     // called from C code
       this.name = "system";
       this.maxPriority = Thread.MAX_PRIORITY;
       this.parent = null;
   }

发现ThreadGroup默认构造方法是private的,是由c调用的,创建的正是system线程组。

批量停止线程

调用线程组interrupt(),会将线程组树下的所有子孙线程中断标志置为true,可以用来批量中断线程。

示例代码:


package com.itsoku.chat02;
import java.util.concurrent.TimeUnit;
/**
* <b>description</b>:<br>
* <b>time</b>:2019/7/13 17:53 <br>
* <b>author</b>:微信公众号:路人甲Java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!
*/
public class Demo4 {
   public static class R1 implements Runnable {
       @Override
       public void run() {
           Thread thread = Thread.currentThread();
           System.out.println("所属线程组:" + thread.getThreadGroup().getName() + ",线程名称:" + thread.getName());
           while (!thread.isInterrupted()) {
               ;
           }
           System.out.println("线程:" + thread.getName() + "停止了!");
       }
   }
   public static void main(String[] args) throws InterruptedException {
       ThreadGroup threadGroup1 = new ThreadGroup("thread-group-1");
       Thread t1 = new Thread(threadGroup1, new R1(), "t1");
       Thread t2 = new Thread(threadGroup1, new R1(), "t2");
       t1.start();
       t2.start();
       ThreadGroup threadGroup2 = new ThreadGroup(threadGroup1, "thread-group-2");
       Thread t3 = new Thread(threadGroup2, new R1(), "t3");
       Thread t4 = new Thread(threadGroup2, new R1(), "t4");
       t3.start();
       t4.start();
       TimeUnit.SECONDS.sleep(1);
       System.out.println("-----------threadGroup1信息-----------");
       threadGroup1.list();
       System.out.println("----------------------");
       System.out.println("停止线程组:" + threadGroup1.getName() + "中的所有子孙线程");
       threadGroup1.interrupt();
       TimeUnit.SECONDS.sleep(2);
       System.out.println("----------threadGroup1停止后,输出信息------------");
       threadGroup1.list();
   }
}

输出:

所属线程组:thread-group-1,线程名称:t1
所属线程组:thread-group-1,线程名称:t2
所属线程组:thread-group-2,线程名称:t3
所属线程组:thread-group-2,线程名称:t4
-----------threadGroup1信息-----------
java.lang.ThreadGroup[name=thread-group-1,maxpri=10]
Thread[t1,5,thread-group-1]
Thread[t2,5,thread-group-1]
java.lang.ThreadGroup[name=thread-group-2,maxpri=10]
Thread[t3,5,thread-group-2]
Thread[t4,5,thread-group-2]
----------------------
停止线程组:thread-group-1中的所有子孙线程
线程:t4停止了!
线程:t2停止了!
线程:t1停止了!
线程:t3停止了!
----------threadGroup1停止后,输出信息------------
java.lang.ThreadGroup[name=thread-group-1,maxpri=10]
java.lang.ThreadGroup[name=thread-group-2,maxpri=10]

停止线程之后,通过list()方法可以看出输出的信息中不包含已结束的线程了。

来源:https://itsoku.blog.csdn.net/article/details/100036280

标签:java,高并发,线程组
0
投稿

猜你喜欢

  • C#如何自定义multipart/form-data的解析器

    2023-12-04 18:59:03
  • Java值传递之swap()方法不能交换的解决

    2023-11-12 20:54:50
  • Java 字符终端上获取输入三种的方式分享

    2021-12-31 04:52:45
  • 深入理解C# 装箱和拆箱(整理篇)

    2023-10-04 02:13:13
  • Android 蓝牙连接 ESC/POS 热敏打印机打印实例(蓝牙连接篇)

    2021-12-02 10:21:18
  • Android创建与解析XML(二)——详解Dom方式

    2023-08-20 02:30:05
  • C#的通用DbHelper类(支持数据连接池)示例详解

    2022-01-14 11:59:56
  • Java农夫过河问题的继承与多态实现详解

    2022-07-12 16:13:22
  • Java中常用的设计模式之装饰器模式详解

    2021-08-28 07:59:15
  • Android 开发之Dialog,Toast,Snackbar提醒

    2021-05-31 14:27:20
  • Java 继承与多态的深入理解

    2023-10-05 04:25:41
  • 适用于WebForm Mvc的Pager分页组件C#实现

    2022-05-11 22:11:34
  • 使用IDEA搭建一个简单的SpringBoot项目超详细过程

    2022-07-19 11:43:58
  • Android自定义控件实现带数值和动画的圆形进度条

    2021-09-09 22:02:46
  • Maven安装及MyEclipse中使用Maven

    2023-06-20 04:29:07
  • C#同步网络时间的方法实例详解

    2021-10-24 15:47:47
  • Android编程之动态壁纸实例分析

    2023-02-23 07:05:15
  • 一篇文章带你入门Java基本概念

    2023-11-26 01:17:08
  • spring cloud alibaba Nacos 注册中心搭建过程详解

    2022-07-08 17:38:01
  • Android中TextView动态设置缩进距离的方法

    2023-08-07 09:52:00
  • asp之家 软件编程 m.aspxhome.com