线程组能够批量管理线程和线程组对象。java
例子以下,创建一级关联。this
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 ······
每隔三秒输出两个线程名称,符合预期。code
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 第一个线程组名称为:新的组
没有指定线程组,则归属到当前线程所属的组。blog
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()); } }
运行结果get
system Exception in thread "main" java.lang.NullPointerException at com.advance.MultiThread3.MyThread.ThreadDomain50.main(ThreadDomain50.java:14)
当前线程的线程组的父线程组是系统线程组;系统线程组的父线程组不存在;系统线程组就是根线程组。io
批量中止组内线程
请看示例class
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()方法"); } }
输出结果以下thread
ThreadName = 线程0准备开始死循环了 ThreadName = 线程1准备开始死循环了 ThreadName = 线程2准备开始死循环了 调用了interrupt()方法 ThreadName = 线程0结束了 ThreadName = 线程2结束了 ThreadName = 线程1结束了
能够看到,ThreadGroup的interrupt方法批量中断线程组的线程。