• 并发编程学习笔记(六、线程组)


    目录:

    • 线程组概念
    • 一级关联
    • 多级关联
    • 线程组自动归属特性
    • 批量管理线程

    线程组概念:

    线程组其实就是线程的一个集合,其中可以包含线程,也可以包含线程组,它和树形结构非常相似。

    线程组可以有效的组织线程和线程组

    一级关联:

    父对象中有子对象,但不存在孙子对象。

     1 public class TestThread implements Runnable {
     2     @Override
     3     public void run() {
     4         try {
     5             while (!Thread.currentThread().isInterrupted()) {
     6                 System.out.println("ThreadName = " + Thread.currentThread().getName());
     7                 Thread.sleep(3000);
     8             }
     9         } catch (InterruptedException e) {
    10             e.printStackTrace();
    11         }
    12     }
    13 }
     1 public class OneLevelDemo {
     2     public static void main(String[] args) {
     3         TestThread testThread1 = new TestThread();
     4         TestThread testThread2 = new TestThread();
     5         ThreadGroup threadGroup = new ThreadGroup("新建线程组1");
     6         Thread t0 = new Thread(threadGroup, testThread1);
     7         Thread t1 = new Thread(threadGroup, testThread2);
     8         t0.start();
     9         t1.start();
    10         System.out.println("活动的线程数为:" + threadGroup.activeCount());
    11         System.out.println("线程组的名称为:" + threadGroup.getName());
    12     }
    13 }

    多级关联:

    父对象中有子对象,也存在孙子对象;但这种级别的线程组是不推荐的,因为维护的成本过高

     1 public class LevelsDemo {
     2     public static void main(String[] args) {
     3         ThreadGroup threadGroup1 = new ThreadGroup("线程组1");
     4         ThreadGroup threadGroup2
     5                 = new ThreadGroup(threadGroup1, "线程组2");
     6         ThreadGroup threadGroup3
     7                 = new ThreadGroup(threadGroup1, "线程组3");
     8         TestThread testThread1 = new TestThread();
     9         TestThread testThread2 = new TestThread();
    10         TestThread testThread3 = new TestThread();
    11         Thread t0 = new Thread(threadGroup1, testThread1);
    12         Thread t1 = new Thread(threadGroup2, testThread2);
    13         Thread t2 = new Thread(threadGroup3, testThread3);
    14         t0.start();
    15         t1.start();
    16         t2.start();
    17         System.out.println("threadGroup1线程组的名称为:" + threadGroup1.getName());
    18         System.out.println("threadGroup1活动的线程数为:" + threadGroup1.activeCount());
    19         System.out.println("threadGroup1活动的线程组数为:" + threadGroup1.activeGroupCount());
    20         System.out.println("threadGroup2线程组的名称为:" + threadGroup2.getName());
    21         System.out.println("threadGroup2活动的线程数为:" + threadGroup2.activeCount());
    22         System.out.println("threadGroup3线程组的名称为:" + threadGroup3.getName());
    23         System.out.println("threadGroup3活动的线程数为:" + threadGroup3.activeCount());
    24     }
    25 }

    线程组自动归属特性:

    若线程组未指定则归属到初始化时所处于的线程中。

     1 public class ThreadGroupAuto {
     2     public static void main(String[] args) {
     3         System.out.println("当前线程:" + Thread.currentThread().getName() 
     4                 + ", 所属线程组:" + Thread.currentThread().getThreadGroup().getName()
     5                 + ", 线程组中有线程组数量:" + Thread.currentThread().getThreadGroup().activeGroupCount());
     6         // 没有指定线程组,那么自动归到当前线程所属的线程组中
     7         ThreadGroup group = new ThreadGroup("新的组");
     8         System.out.println("线程组中有线程组数量:"
     9                 + Thread.currentThread().getThreadGroup().activeGroupCount());
    10     }
    11 }

    批量管理线程:

    通过interrupt()方法中断那些长时间还未执行完的线程。

     1 public class BatchThread extends Thread {
     2     public BatchThread(ThreadGroup tg, String name) {
     3         super(tg, name);
     4     }
     5 
     6     @Override
     7     public void run() {
     8         System.out.println("ThreadName = " + Thread.currentThread().getName() + "开始死循环了");
     9         while (!this.isInterrupted()) {
    10         }
    11         System.out.println("ThreadName = " + Thread.currentThread().getName() + "结束了");
    12     }
    13 }
     1 public class ThreadGroupBatchDemo {
     2     public static void main(String[] args) throws InterruptedException {
     3         int batch = 5;
     4         ThreadGroup threadGroup = new ThreadGroup("我的线程组");
     5         for (int i = 0; i < batch; i++) {
     6             BatchThread batchThread
     7                     = new BatchThread(threadGroup, "线程" + i);
     8             batchThread.start();
     9         }
    10         Thread.sleep(10000);
    11         threadGroup.interrupt();
    12         System.out.println("调用了ThreadGroup.interrupt()方法");
    13     }
    14 }
  • 相关阅读:
    Mysql 数据库学习笔记03 存储过程
    Mysql 数据库学习笔记02 编程
    Mysql 数据库学习笔记01查询
    Struts2学习笔记04 之 拦截器
    Struts2学习笔记03 之 Result组件
    Group by与having理解
    Spring Assert断言工具类
    字符集、编码
    hibernate中Query的list和iterator区别(续)
    hibernate中Query的list和iterator区别
  • 原文地址:https://www.cnblogs.com/bzfsdr/p/11574684.html
Copyright © 2020-2023  润新知