Java多线程 线程组原理及实例详解

线程组

Java多线程 线程组原理及实例详解_第1张图片

线程组可以批量管理线程和线程组对象。

一级关联

例子如下,建立一级关联。

public class MyThread43 implements Runnable{
  public void run()
  {
    try
    {
      while (!Thread.currentThread().isInterrupted())
      {
        System.out.println("ThreadName = " + Thread.currentThread().getName());
        Thread.sleep(3000);
      }
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
  }

  public static void main(String[] args)
  {
    MyThread43 mt0 = new MyThread43();
    MyThread43 mt1 = new MyThread43();
    ThreadGroup tg = new ThreadGroup("新建线程组1");
    Thread t0 = new Thread(tg, mt0);
    Thread t1 = new Thread(tg, mt1);
    t0.start();
    t1.start();
    System.out.println("活动的线程数为:" + tg.activeCount());
    System.out.println("线程组的名称为:" + tg.getName());
  }
}

输出结果如下

活动的线程数为:2
线程组的名称为:新建线程组1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-0
・・・・・・

每隔三秒输出两个线程名称,符合预期。

线程组自动归组属性

public class ThreadDomain49 {
  public static void main(String[] args) {
    System.out.println("A处线程:" + Thread.currentThread().getName() + ", 所属线程:" + Thread.currentThread().getThreadGroup().getName() +
        ", 组中有线程组数量:" + Thread.currentThread().getThreadGroup().activeGroupCount());
    ThreadGroup group = new ThreadGroup("新的组");
    System.out.println("B处线程:" + Thread.currentThread().getName() + ", 所属线程:" + Thread.currentThread().getThreadGroup().getName() +
        ", 组中有线程组数量:" + Thread.currentThread().getThreadGroup().activeGroupCount());
    ThreadGroup[] tg = new ThreadGroup[Thread.currentThread().getThreadGroup().activeGroupCount()];
    Thread.currentThread().getThreadGroup().enumerate(tg);
    for (int i = 0; i < tg.length; i++)
      System.out.println("第一个线程组名称为:" + tg[i].getName());
  }
}

输出结果如下

A处线程:main, 所属线程:main, 组中有线程组数量:0
B处线程:main, 所属线程:main, 组中有线程组数量:1
第一个线程组名称为:新的组

没有指定线程组,则归属到当前线程所属的组。

根线程组

public class ThreadDomain50 {
  public static void main(String[] args)
  {
    System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName());
  }
}

运行结果

system
Exception in thread "main" java.lang.NullPointerException
  at com.advance.MultiThread3.MyThread.ThreadDomain50.main(ThreadDomain50.java:14)

当前线程的线程组的父线程组是系统线程组;系统线程组的父线程组不存在;系统线程组就是根线程组。

批量停止组内线程

请看示例

public class MyThread44 extends Thread{

  public MyThread44(ThreadGroup tg, String name)
  {
    super(tg, name);
  }

  public void run()
  {
    System.out.println("ThreadName = " + Thread.currentThread().getName() +
        "准备开始死循环了");
    while (!this.isInterrupted()){}
    System.out.println("ThreadName = " + Thread.currentThread().getName() +
        "结束了");
  }

  public static void main(String[] args) throws InterruptedException {
    ThreadGroup tg = new ThreadGroup("我的线程组");
    MyThread44 mt = null;
    for (int i = 0; i < 3; i++)
    {
      mt = new MyThread44(tg, "线程" + i);
      mt.start();
    }
    Thread.sleep(5000);
    tg.interrupt();
    System.out.println("调用了interrupt()方法");
  }
}

输出结果如下

ThreadName = 线程0准备开始死循环了
ThreadName = 线程1准备开始死循环了
ThreadName = 线程2准备开始死循环了
调用了interrupt()方法
ThreadName = 线程0结束了
ThreadName = 线程2结束了
ThreadName = 线程1结束了

可以看到,ThreadGroup的interrupt方法批量中断线程组的线程。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Java多线程 线程组原理及实例详解)