第8篇:线程组

star2017 1年前 ⋅ 518 阅读

线程组

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

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

创建线程关联线程组

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

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

输出结果:

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

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

为线程组指定父线程组

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

  1. public ThreadGroup(String name)
  2. public ThreadGroup(ThreadGroup parent, String name)

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

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

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

上一段示例代码:

  1. package com.itsoku.chat02;
  2. import java.util.concurrent.TimeUnit;
  3. /**
  4. * <b>description</b>: <br>
  5. * <b>time</b>:2019/7/13 17:53 <br>
  6. * <b>author</b>:微信公众号:路人甲Java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!
  7. */
  8. public class Demo2 {
  9. public static class R1 implements Runnable {
  10. @Override
  11. public void run() {
  12. Thread thread = Thread.currentThread();
  13. System.out.println("所属线程组:" + thread.getThreadGroup().getName() + ",线程名称:" + thread.getName());
  14. try {
  15. TimeUnit.SECONDS.sleep(3);
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. public static void main(String[] args) throws InterruptedException {
  22. ThreadGroup threadGroup1 = new ThreadGroup("thread-group-1");
  23. Thread t1 = new Thread(threadGroup1, new R1(), "t1");
  24. Thread t2 = new Thread(threadGroup1, new R1(), "t2");
  25. t1.start();
  26. t2.start();
  27. TimeUnit.SECONDS.sleep(1);
  28. System.out.println("threadGroup1活动线程数:" + threadGroup1.activeCount());
  29. System.out.println("threadGroup1活动线程组:" + threadGroup1.activeGroupCount());
  30. System.out.println("threadGroup1线程组名称:" + threadGroup1.getName());
  31. System.out.println("threadGroup1父线程组名称:" + threadGroup1.getParent().getName());
  32. System.out.println("----------------------");
  33. ThreadGroup threadGroup2 = new ThreadGroup(threadGroup1, "thread-group-2");
  34. Thread t3 = new Thread(threadGroup2, new R1(), "t3");
  35. Thread t4 = new Thread(threadGroup2, new R1(), "t4");
  36. t3.start();
  37. t4.start();
  38. TimeUnit.SECONDS.sleep(1);
  39. System.out.println("threadGroup2活动线程数:" + threadGroup2.activeCount());
  40. System.out.println("threadGroup2活动线程组:" + threadGroup2.activeGroupCount());
  41. System.out.println("threadGroup2线程组名称:" + threadGroup2.getName());
  42. System.out.println("threadGroup2父线程组名称:" + threadGroup2.getParent().getName());
  43. System.out.println("----------------------");
  44. System.out.println("threadGroup1活动线程数:" + threadGroup1.activeCount());
  45. System.out.println("threadGroup1活动线程组:" + threadGroup1.activeGroupCount());
  46. System.out.println("----------------------");
  47. threadGroup1.list();
  48. }
  49. }

输出结果:

  1. 所属线程组:thread-group-1,线程名称:t1
  2. 所属线程组:thread-group-1,线程名称:t2
  3. threadGroup1活动线程数:2
  4. threadGroup1活动线程组:0
  5. threadGroup1线程组名称:thread-group-1
  6. threadGroup1父线程组名称:main
  7. ----------------------
  8. 所属线程组:thread-group-2,线程名称:t4
  9. 所属线程组:thread-group-2,线程名称:t3
  10. threadGroup2活动线程数:2
  11. threadGroup2活动线程组:0
  12. threadGroup2线程组名称:thread-group-2
  13. threadGroup2父线程组名称:thread-group-1
  14. ----------------------
  15. threadGroup1活动线程数:4
  16. threadGroup1活动线程组:1
  17. ----------------------
  18. java.lang.ThreadGroup[name=thread-group-1,maxpri=10]
  19. Thread[t1,5,thread-group-1]
  20. Thread[t2,5,thread-group-1]
  21. java.lang.ThreadGroup[name=thread-group-2,maxpri=10]
  22. Thread[t3,5,thread-group-2]
  23. Thread[t4,5,thread-group-2]

代码解释:

  1. threadGroup1未指定父线程组,系统获取了主线程的线程组作为threadGroup1的父线程组,输出结果中是:main
  2. threadGroup1为threadGroup2的父线程组
  3. threadGroup1活动线程数为4,包含了threadGroup1线程组中的t1、t2,以及子线程组threadGroup2中的t3、t4
  4. 线程组的list()方法,将线程组中的所有子孙节点信息输出到控制台,用于调试使用

根线程组

获取根线程组

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

运行上面代码,输出:

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

从上面代码可以看出:

  1. 主线程的线程组为main
  2. 根线程组为system

看一下ThreadGroup的源码:

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

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

批量停止线程

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

示例代码:

  1. package com.itsoku.chat02;
  2. import java.util.concurrent.TimeUnit;
  3. /**
  4. * <b>description</b>: <br>
  5. * <b>time</b>:2019/7/13 17:53 <br>
  6. * <b>author</b>:微信公众号:路人甲Java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!
  7. */
  8. public class Demo4 {
  9. public static class R1 implements Runnable {
  10. @Override
  11. public void run() {
  12. Thread thread = Thread.currentThread();
  13. System.out.println("所属线程组:" + thread.getThreadGroup().getName() + ",线程名称:" + thread.getName());
  14. while (!thread.isInterrupted()) {
  15. ;
  16. }
  17. System.out.println("线程:" + thread.getName() + "停止了!");
  18. }
  19. }
  20. public static void main(String[] args) throws InterruptedException {
  21. ThreadGroup threadGroup1 = new ThreadGroup("thread-group-1");
  22. Thread t1 = new Thread(threadGroup1, new R1(), "t1");
  23. Thread t2 = new Thread(threadGroup1, new R1(), "t2");
  24. t1.start();
  25. t2.start();
  26. ThreadGroup threadGroup2 = new ThreadGroup(threadGroup1, "thread-group-2");
  27. Thread t3 = new Thread(threadGroup2, new R1(), "t3");
  28. Thread t4 = new Thread(threadGroup2, new R1(), "t4");
  29. t3.start();
  30. t4.start();
  31. TimeUnit.SECONDS.sleep(1);
  32. System.out.println("-----------threadGroup1信息-----------");
  33. threadGroup1.list();
  34. System.out.println("----------------------");
  35. System.out.println("停止线程组:" + threadGroup1.getName() + "中的所有子孙线程");
  36. threadGroup1.interrupt();
  37. TimeUnit.SECONDS.sleep(2);
  38. System.out.println("----------threadGroup1停止后,输出信息------------");
  39. threadGroup1.list();
  40. }
  41. }

输出:

  1. 所属线程组:thread-group-1,线程名称:t1
  2. 所属线程组:thread-group-1,线程名称:t2
  3. 所属线程组:thread-group-2,线程名称:t3
  4. 所属线程组:thread-group-2,线程名称:t4
  5. -----------threadGroup1信息-----------
  6. java.lang.ThreadGroup[name=thread-group-1,maxpri=10]
  7. Thread[t1,5,thread-group-1]
  8. Thread[t2,5,thread-group-1]
  9. java.lang.ThreadGroup[name=thread-group-2,maxpri=10]
  10. Thread[t3,5,thread-group-2]
  11. Thread[t4,5,thread-group-2]
  12. ----------------------
  13. 停止线程组:thread-group-1中的所有子孙线程
  14. 线程:t4停止了!
  15. 线程:t2停止了!
  16. 线程:t1停止了!
  17. 线程:t3停止了!
  18. ----------threadGroup1停止后,输出信息------------
  19. java.lang.ThreadGroup[name=thread-group-1,maxpri=10]
  20. java.lang.ThreadGroup[name=thread-group-2,maxpri=10]

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

多说几句,建议大家再创建线程或者线程组的时候,给他们取一个有意义的名字,对于计算机来说,可能名字并不重要,但是在系统出问题的时候,你可能会去查看线程堆栈信息,如果你看到的都是t1、t2、t3,估计自己也比较崩溃,如果看到的是httpAccpHandler、dubboHandler类似的名字,应该会好很多。

更多好文章

  1. spring高手系列(正在连载中)

最新资料

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: