Analysis of ThreadGroup Class in Java

Keywords: Java JDK

The ThreadGroup class is used in Java to represent a thread group, representing a set of threads, which can be managed by a group of threads and thread groups. Threads can be attributed to a thread group, which can have thread objects, thread groups and threads. Such an organization structure is somewhat similar to that of a tree, as shown in the figure.

 

All threads created by the user belong to the specified thread group. If no thread group is explicitly specified, the thread belongs to the default thread group (that is, the main thread group). By default, the child thread and the parent thread are in the same thread group.

In addition, only when a thread is created can the thread group it belongs to be specified. Threads cannot change the thread group they belong to in the middle of running, that is to say, once a thread specifies the thread group it belongs to, it cannot be changed.

2. Why use thread groups

1. safety

Threads of the same thread group can modify each other's data. However, if in different thread groups, it is impossible to "cross-thread group" to modify data, which can ensure data security to a certain extent.

2. Batch management

Threads or thread group objects can be managed in batches to organize or control threads or thread group objects effectively.

3. Examples of Thread Group Use

Thread Associated Thread Groups: Level 1 Associations

The so-called first-level association is that the parent object has children, but does not create grandchildren. For example, create a thread group and then assign the created threads to that group, so that these threads can be effectively managed. The 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() {
 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 code

The results are as follows:

Thread Name: Thread A, Thread Group: root Thread Group
Thread Name: Thread B, Thread Group: root Thread Group
Copy code

2. Thread Associated Thread Groups: Multilevel Associations

The so-called multi-level association is that there are children in the parent object, and the creation of grandchildren in the child object will have the effect of descendants. For example, using the second construction method in the following figure, we can assign the sub-thread group to a thread group, and then the created thread to the sub-thread group, which will have the effect of the thread tree.

 

The 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, "Sub thread group");
 Thread thread2 = new Thread(threadGroup1, new MRunnable(), "thread C");
 Thread thread3 = new Thread(threadGroup1, new MRunnable(), "thread D");
 thread2.start();
 thread3.start();
 }
}
class MRunnable implements Runnable {
 @Override
 public void run() {
 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 code

The results are as follows:

Thread name: thread A, thread group: root thread group, parent thread group: main
Thread name: Thread B, where thread group: root thread group, parent thread group: main
Thread Name: Thread C, Thread Group: Subthread Group, Parent Thread Group: root Thread Group
Thread Name: Thread D, Thread Group: Subthread Group, Parent Thread Group: root Thread Group
Copy code

3. Threads in batch management group

Thread groups are naturally used to manage threads in batches, such as interrupting threads in groups in batches. The 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, "Sub thread group");
 Thread thread2 = new Thread(threadGroup1, new MRunnable(), "thread C");
 Thread thread3 = new Thread(threadGroup1, new MRunnable(), "thread D");
 thread2.start();
 thread3.start();
 rootThreadGroup.interrupt();
 System.out.println("Batch interrupt intra-group threads");
 }
}
class MRunnable implements Runnable {
 @Override
 public void run() {
 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 code

The results are as follows:

Thread name: thread A, thread group: root thread group, parent thread group: main
Thread name: Thread B, where thread group: root thread group, parent thread group: main
Thread Name: Thread C, Thread Group: Subthread Group, Parent Thread Group: root Thread Group
Thread Name: Thread D, Thread Group: Subthread Group, Parent Thread Group: root Thread Group
Batch interrupt intra-group threads
Thread A Execution End
Thread B Execution End
Thread C Execution End
Thread D Execution End
Copy code

This article is just a brief introduction and demonstration of the ThreadGroup class in Java. You can see the JDK API for more thread group operations.

Reference resources:

https://www.cnblogs.com/xrq730/p/4856072.html

https://www.cnblogs.com/barrywxx/p/9976417.html

Finally, here are some structure information to share with you!

Please add 772300343 to get it!

Thank you for your attention!

Posted by jredsmyth on Thu, 12 Sep 2019 01:54:11 -0700