A concept.
Java uses the ThreadGroup class to represent a ThreadGroup, representing a collection of threads that can be managed by a batch of threads and thread groups. Threads can be grouped into a thread group, which can have thread objects, thread groups, and threads within the group. This kind of organizational structure is similar to a tree, as shown in the figure.
All threads created by the user belong to the specified thread group, and if they are not explicitly specified, they belong to the default thread group (that is, the main thread group). By default, the child thread and parent thread are in the same thread group.
In addition, the thread group can only be specified when a thread is created, and the thread group cannot be changed halfway through its run, meaning that a thread cannot change the thread group once it is specified.
Why use thread groups
1. The security
Threads in the same thread group can modify each other’s data. However, if the data is in different thread groups, it cannot be modified “across thread groups”, thus ensuring data security to a certain extent.
2. Batch management
Threads or thread group objects can be managed in batches, effectively organizing or controlling them.
Example of using thread groups
1. Thread association Thread group: Level 1 association
A first-level association is one in which a parent object has children but does not create grandchildren. For example, you can create a thread group and then assign the created threads to that group to manage these threads effectively. A code example is as follows:
public class ThreadGroupTest {
public static void main(String[] args) {
ThreadGroup rootThreadGroup = new ThreadGroup("Root thread Group");
Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "Thread A");
Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "Thread B"); thread0.start(); thread1.start(); }}class MRunnable implements Runnable {
@Override
public void run(a) {
while(! Thread.currentThread().isInterrupted()) { System.out.println("Thread name:" + Thread.currentThread().getName()
+ ", thread group:" + Thread.currentThread().getThreadGroup().getName()) ;
try {
Thread.sleep(1000);
} catch(InterruptedException e) { e.printStackTrace(); }}}}Copy the code
The result is as follows:
Thread name: thread A, thread group: root Thread group Thread name: thread B, thread group: root thread groupCopy the code
2. Thread association Thread group: multi-level association
The so-called multi-level association is that there are children in the parent object, and then create grandchildren in the child object will appear the effect of descendants. For example, using the second constructor in the figure below, you can assign a subthread group to a subthread group and then assign the created thread to a subthread group. This gives you the effect of a thread tree.
public class ThreadGroupTest {
public static void main(String[] args) {
ThreadGroup rootThreadGroup = new ThreadGroup("Root thread Group");
Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "Thread A");
Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "Thread B");
thread0.start();
thread1.start();
ThreadGroup threadGroup1 = new ThreadGroup(rootThreadGroup, "Child thread group");
Thread thread2 = new Thread(threadGroup1, new MRunnable(), Thread "C");
Thread thread3 = new Thread(threadGroup1, new MRunnable(), Threads "D"); thread2.start(); thread3.start(); }}class MRunnable implements Runnable {
@Override
public void run(a) {
while(! Thread.currentThread().isInterrupted()) { System.out.println("Thread name:" + Thread.currentThread().getName()
+ ", thread group:" + Thread.currentThread().getThreadGroup().getName()
+ Parent thread group: + Thread.currentThread().getThreadGroup().getParent().getName());
try {
Thread.sleep(1000);
} catch(InterruptedException e) { e.printStackTrace(); }}}}Copy the code
The result is as follows:
Thread name: Thread A, thread group: root thread group, parent thread group: main Thread name: thread B, thread group: root thread group, parent thread group: main Thread name: thread C, thread group: child thread group, parent thread group: root thread group Thread D belongs to the child thread group and parent thread group is root thread groupCopy the code
3. Manage group threads in batches
Using thread groups is naturally to manage threads in batches, such as batch interrupt group threads, code example is as follows:
public class ThreadGroupTest {
public static void main(String[] args) {
ThreadGroup rootThreadGroup = new ThreadGroup("Root thread Group");
Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "Thread A");
Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "Thread B");
thread0.start();
thread1.start();
ThreadGroup threadGroup1 = new ThreadGroup(rootThreadGroup, "Child thread group");
Thread thread2 = new Thread(threadGroup1, new MRunnable(), Thread "C");
Thread thread3 = new Thread(threadGroup1, new MRunnable(), Threads "D");
thread2.start();
thread3.start();
rootThreadGroup.interrupt();
System.out.println("Batch interrupt group threads"); }}class MRunnable implements Runnable {
@Override
public void run(a) {
while(! Thread.currentThread().isInterrupted()) { System.out.println("Thread name:" + Thread.currentThread().getName()
+ ", thread group:" + Thread.currentThread().getThreadGroup().getName()
+ Parent thread group: + Thread.currentThread().getThreadGroup().getParent().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
System.out.println(Thread.currentThread().getName() + "End of execution"); }}Copy the code
The result is as follows:
Thread name: Thread A, thread group: root thread group, parent thread group: main Thread name: thread B, thread group: root thread group, parent thread group: main Thread name: thread C, thread group: child thread group, parent thread group: root thread group Thread GROUP: child thread group, parent thread group: root Thread group Batch interrupt Thread within the group Thread A Finish thread B Finish thread C finish thread D Finish threadCopy the code
This article has provided a brief introduction to and demonstration of the ThreadGroup class in Java. You can see the JDK API for more details on how to operate multithreaded groups.
Reference:
Java Multithreading 16: Thread groups
Java thread groups