浅析Java中线程组(ThreadGroup类)

一.概念

Java中使用ThreadGroup类来代表线程组,表示一组线程的集合,可以对一批线程和线程组进行管理。可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线程,这样的组织结构有点类似于树的形式,如图所示。

线程组

用户创建的所有线程都属于指定线程组,如果没有显式指定属于哪个线程组,那么该线程就属于默认线程组(即main线程组)。默认情况下,子线程和父线程处于同一个线程组。

此外,只有在创建线程时才能指定其所在的线程组,线程运行中途不能改变它所属的线程组,也就是说线程一旦指定所在的线程组就不能改变。

二.为什么要使用线程组

1.安全

同一个线程组的线程是可以相互修改对方的数据的。但如果在不同的线程组中,那么就不能“跨线程组”修改数据,可以从一定程度上保证数据安全。

2.批量管理

可以批量管理线程或线程组对象,有效地对线程或线程组对象进行组织或控制。

三.线程组使用示例

1.线程关联线程组:一级关联

所谓一级关联就是父对象中有子对象,但并不创建孙对象。比如创建一个线程组,然后将创建的线程归属到该组中,从而对这些线程进行有效的管理。代码示例如下:

public class ThreadGroupTest {
    public static void main(String[] args) {
        ThreadGroup rootThreadGroup = new ThreadGroup("root线程组");
        Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "线程A");
        Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "线程B");
        thread0.start();
        thread1.start();
    }
}

class MRunnable implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            System.out.println("线程名: " + Thread.currentThread().getName() 
+ ", 所在线程组: " + Thread.currentThread().getThreadGroup().getName()) ;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
复制代码

执行结果如下:

线程名: 线程A, 所在线程组: root线程组
线程名: 线程B, 所在线程组: root线程组
复制代码

2.线程关联线程组:多级关联

所谓的多级关联就是父对象中有子对象,子对象中再创建孙对象也就出现了子孙的效果了。比如使用下图第二个构造方法,将子线程组归属到某个线程组,再将创建的线程归属到子线程组,这样就会有线程树的效果了。

构造方法
代码示例如下:

public class ThreadGroupTest {
    public static void main(String[] args) {
        ThreadGroup rootThreadGroup = new ThreadGroup("root线程组");
        Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "线程A");
        Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "线程B");
        thread0.start();
        thread1.start();
        ThreadGroup threadGroup1 = new ThreadGroup(rootThreadGroup, "子线程组");
        Thread thread2 = new Thread(threadGroup1, new MRunnable(), "线程C");
        Thread thread3 = new Thread(threadGroup1, new MRunnable(), "线程D");
        thread2.start();
        thread3.start();
    }
}

class MRunnable implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            System.out.println("线程名: " + Thread.currentThread().getName()
                    + ", 所在线程组: " + Thread.currentThread().getThreadGroup().getName()
                    + ", 父线程组: " + Thread.currentThread().getThreadGroup().getParent().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
复制代码

执行结果如下:

线程名: 线程A, 所在线程组: root线程组, 父线程组: main
线程名: 线程B, 所在线程组: root线程组, 父线程组: main
线程名: 线程C, 所在线程组: 子线程组, 父线程组: root线程组
线程名: 线程D, 所在线程组: 子线程组, 父线程组: root线程组
复制代码

3.批量管理组内线程

使用线程组自然是要对线程进行批量管理,比如可以批量中断组内线程,代码示例如下:

public class ThreadGroupTest {
    public static void main(String[] args) {
        ThreadGroup rootThreadGroup = new ThreadGroup("root线程组");
        Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "线程A");
        Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "线程B");
        thread0.start();
        thread1.start();
        ThreadGroup threadGroup1 = new ThreadGroup(rootThreadGroup, "子线程组");
        Thread thread2 = new Thread(threadGroup1, new MRunnable(), "线程C");
        Thread thread3 = new Thread(threadGroup1, new MRunnable(), "线程D");
        thread2.start();
        thread3.start();
        rootThreadGroup.interrupt();
        System.out.println("批量中断组内线程");
    }
}

class MRunnable implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            System.out.println("线程名: " + Thread.currentThread().getName()
                    + ", 所在线程组: " + Thread.currentThread().getThreadGroup().getName()
                    + ", 父线程组: " + Thread.currentThread().getThreadGroup().getParent().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
        System.out.println(Thread.currentThread().getName() + "执行结束");
    }
}
复制代码

执行结果如下:

线程名: 线程A, 所在线程组: root线程组, 父线程组: main
线程名: 线程B, 所在线程组: root线程组, 父线程组: main
线程名: 线程C, 所在线程组: 子线程组, 父线程组: root线程组
线程名: 线程D, 所在线程组: 子线程组, 父线程组: root线程组
批量中断组内线程
线程A执行结束
线程B执行结束
线程C执行结束
线程D执行结束
复制代码

本文只是对Java中的ThreadGroup类进行了简单的介绍和使用示范,更多线程组的操作可以查看JDK API。

参考:

Java多线程16:线程组

Java线程组(ThreadGroup)使用

猜你喜欢

转载自juejin.im/post/5d79c17ae51d4561b674c4f1