On the Java thread group (ThreadGroup class)

A concept

Java is used to represent the thread group ThreadGroup class, represents a collection of threads, can manage a number of threads and thread groups. Can be attributed to the thread in a thread group, there may be a thread in the thread group objects, can have a thread group, the group may also be a thread, such organizational structure somewhat similar to a tree, as shown in FIG.

Thread Group

All threads are created by the user belongs to the specified thread group, if not explicitly specify which group belongs to a thread, then the thread part of the default thread group (ie the main thread group). By default, the father of the child thread and the thread is in the same thread group.

In addition, only specified when creating a thread in its thread group, thread running half-way can not change the thread group to which it belongs, that is to say the specified thread group where the thread can not be changed once.

II. Why use thread groups

1. Security

Threads with a thread group can modify each other's data. However, if in a different thread group, then it can not "cross-thread group" modify data, data security can be guaranteed to some extent.

2. Batch Management

Thread or threads can batch management group objects, effectively thread or thread group objects organization or control.

III. Examples of the use of thread groups

1. Thread thread affinity group: an association

Association is called a parent has child objects, but do not create the Sun object. For example, to create a thread group, and then create a thread belonging to the group, so these threads effective management. Code examples are as follows:

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();
            }
        }
    }
}
复制代码

Execution results are as follows:

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

2. Thread the associated thread group: multi-level association

The so-called multi-level association is the parent has child objects, the child objects and then create an object also appeared in the Sun descendants of the effect. For example, the second constructor uses the following figure, the child thread group belonging to a thread group, and then create a thread to the child thread group's home, which would have the effect of a thread tree.

Construction method
Code examples are as follows:

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();
            }
        }
    }
}
复制代码

Execution results are as follows:

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

3. Batch Management Group within threads

Use the thread to thread a Natural batch management, such as batch can interrupt the group of threads, the code examples are as follows:

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() + "执行结束");
    }
}
复制代码

Execution results are as follows:

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

This article is for Java ThreadGroup class in a brief introduction and use of demonstration, for more thread group can view JDK API.

reference:

Java multi-threaded 16: Thread Group

Java thread group (ThreadGroup) use

Guess you like

Origin juejin.im/post/5d79c17ae51d4561b674c4f1