首页 > 编程 > Java > 正文

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

2019-11-26 08:37:16
字体:
来源:转载
供稿:网友

线程组

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

一级关联

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

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线程组的名称为:新建线程组1ThreadName = Thread-0ThreadName = Thread-1ThreadName = Thread-0ThreadName = Thread-1ThreadName = Thread-1ThreadName = Thread-0ThreadName = Thread-1ThreadName = 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, 组中有线程组数量:0B处线程: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());  }}

运行结果

systemException 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方法批量中断线程组的线程。

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表