Threads can be assigned to a thread group, thread groups can be threaded, or thread groups, and threads can also be wired in groups. Such an organizational structure is somewhat similar to the form of a tree.
The role of thread groups is that you can manage threads or thread group objects in batches, effectively organizing threads or thread group objects.
1. Thread Object Association Thread Group: Level 1 Association
A 1-level association is a child object in a parent object, but does not create a descendant object. This situation often occurs in development, such as when creating threads, in order to effectively organize these threads, typically creating a thread group and then attributing some of the threads to the group. Such processing can effectively organize and plan the scattered thread objects.
public class MyThread1 extends Thread { @Override public void Run () { try{while (! Thread.CurrentThread (). isinterrupted ()) { System.out.println ("threadname=" + thread.currentthread (). GetName ()); Thread.Sleep (4000); } } catch (Interruptedexception e) { e.printstacktrace ();}} }
public Class MyThread2 extends thread{@Override public void Run () {try{while (! Thread.CurrentThread (). isinterrupted ()) {System.out.println ("threadname=" + Thread. CurrentThread (). GetName ()); Thread.Sleep (4000); }} catch (Interruptedexception e) {e.printstacktrace (); } }}
public class Run {public static void Main (string[] args) throws interruptedexception { MyThread1 t1 = new Mythrea D1 (); MyThread2 t2 = new MyThread2 (); Threadgroup Group = new Threadgroup ("LCW thread Group"); Thread athread = new Thread (group, T1); Thread bthread = new Thread (group, T2); Athread.start (); Bthread.start (); System.out.println ("Number of threads active:" + group.activecount ()); Thread.Sleep (+); System.out.println ("The name of the thread group is:" + group.getname ());} }
The number of threads active is: 2 Threadname=thread-3 Threadname=thread-2 The name of the thread group is: LCW Thread Group Threadname=thread-3 Threadname=thread-2 Threadname=thread-3 Threadname=thread-2 ...... |
The information printed in the console indicates that there are two threads in the thread group, and the name of the thread group is printed out. In addition, two threads have been indefinitely and print logs every 3 seconds.
2. Thread Object Association Thread Group: Multilevel Association
The so-called Multilevel Association is the child object in the parent object, and the child object is then created, that is, the effect of descendant objects. However, this type of writing is less common in development, but the JDK provides a thread tree structure that supports multi-level associations if the thread tree structure is designed to be very complex and not conducive to the management of thread objects.
/** * Add a thread group A to the main group, and then add the thread object x and Y * methods in this group A to the value of Activegroupcount () and Activecount () is not fixed, * is a snapshot of the environment in the system */public class Run { public static void Main (string[] args) throws Interruptedexception {Threadgroup Maingroup = Thread.CurrentThread ( ). Getthreadgroup (); Threadgroup (parent thread Group, child thread group name) Threadgroup Group = new Threadgroup (Maingroup, "AA"); Runnable Runnable = new Runnable () {public void run () {try {System.out.pri Ntln ("runmethod!"); Thread.Sleep (1000); The thread must be in a running state before it can be managed by the group} catch (Interruptedexception e) {e.printstacktrace (); } } }; Thread newthread = new Thread (group, runnable); Newthread.setname ("X"); Newthread.start (); The thread must be started before it is returned to group A thread newThread2 = new Thread (group, runnable); Newthread2.setname ("Y"); Newthread2.start (); The thread must be started before it is returned to group A//threadgroup[] Listgroup =New Threadgroup[thread.currentthread (). Getthreadgroup (). Activegroupcount ()]; Thread.CurrentThread (). Getthreadgroup (). Enumerate (Listgroup); System.out.println ("The number of child thread groups in the main thread:" + Listgroup.length + ", named:" +listgroup[0].getname () + ", how many threads of this child thread group:" + Listgroup[0].activecount ()); thread[] Listthread = new Thread[listgroup[0].activecount ()]; Listgroup[0].enumerate (Listthread); System.out.println (Listthread[0].getname ()); System.out.println (Listthread[1].getname ()); }}
How many child thread groups are in the main thread: 1, the name is: AA, how many threads are there in this child thread group: 2 X Y runmethod! runmethod! |
The structure of this program code is that the main group creates a new group, and then adds threads to the new group.
3, Thread Group automatic attribution feature
Automatic attribution is automatically attributed to the current thread group.
/** * Method Activegroupcount () Gets the number of child thread groups in the current thread Group Object * Method Enumerate () is used to copy the child threads in the thread group to the threadgroup[] Array object in the form of a plural */public class Run {public static void main (string[] args) throws Interruptedexception {System.out.println ("A thread Name:" + Thread.) CurrentThread (). GetName () + ", the thread group that belongs to is named:" +thread.currentthread (). Getthreadgroup (). GetName () + ", number of threads in a thread group:" +thread.currentthread (). Getthreadgroup (). Activegroupcount ()); Threadgroup groups = new Threadgroup ("new Group"); Automatically added to the main group SYSTEM.OUT.PRINTLN ("thread name at B:" + Thread.CurrentThread (). GetName () + ", the thread group that belongs to is named:" +thread.cu Rrentthread (). Getthreadgroup (). GetName () + ", number of threads in a thread group:" +thread.currentthread (). Getthreadgroup (). Activegroup Count ()); threadgroup[] threadgroups = new Threadgroup[thread.currentthread (). Getthreadgroup (). Activegroupcount ()]; Thread.CurrentThread (). Getthreadgroup (). Enumerate (threadgroups); System.out.println ("The first thread group name is:" +threadgroups[0].getname ()); }}
A thread name: Main, which belongs to the thread group named: Main, number of threads in thread Group: 0 b Thread Name: Main, the name of the thread group to which it belongs: Main, number of wired groups: 1 The first thread group name is: New Group |
This experiment proves that if you do not specify a thread group to which you belong when instantiating a Threadgroup thread group x, the X thread group is automatically grouped into the thread group that the current thread object belongs to, that is, implicitly adding a child thread group to a thread group. So the number of thread groups printed in the console has changed from 0 to 1.
4. Get the root thread group
public class Run {public static void Main (string[] args) throws Interruptedexception { System.out.println ("Thread:" +thread.currentthread (). GetName () + ", where the thread group name is:" +thread.currentthread (). Getthreadgroup (). GetName ()); System.out.println ("The name of the parent thread group for the thread group where the main thread is located is:" +thread.currentthread (). Getthreadgroup (). GetParent (). GetName () ); System.out.println ("The name of your thread group for the parent thread group of all thread groups of the main thread is:" +thread.currentthread (). Getthreadgroup (). GetParent (). GetParent (). GetName ());} }
Thread: Main, where the thread group name is: main The name of the parent thread group for the thread group where the main thread is located is: System Exception in thread "main" java.lang.NullPointerException |
The result of the operation shows that the JVM's root thread group is system, and then the parent thread group appears empty exception.
5. Thread Group Riga Thread Group
public Class Run {public static void main (string[] args) throws Interruptedexception {System.out.println ("thread Group Name:" + Thread.CurrentThread (). Getthreadgroup (). GetName ()); System.out.println ("Number of threads active in the thread group:" +thread.currentthread (). Getthreadgroup (). Activecount ()); SYSTEM.OUT.PRINTLN ("Thread Group" data-plus before: "+thread.currentthread (). Getthreadgroup (). Activegroupcount ()); Threadgroup newgroup = new Threadgroup (Thread.CurrentThread (). Getthreadgroup (), "newgroup"); System.out.println ("Number of threads group in thread group-plus:" +thread.currentthread (). Getthreadgroup (). Activegroupcount ()); SYSTEM.OUT.PRINTLN ("Parent Thread Group Name:" +thread.currentthread (). Getthreadgroup (). GetParent (). GetName ()); }}
Thread Group name: main Number of threads active in the thread group: 2 Thread Group data--plus previous: 0 Number of threads in thread group--Plus: 1 Parent Thread Group Name: System |
6, the group of threads in bulk stop
public class MyThread extends Thread {public MyThread (threadgroup Group, String name) { Super (group, name); } @Override public void Run () { System.out.println ("threadname=" + thread.currentthread (). GetName () + ", ready to begin the cycle of Death"); while (!this.isinterrupted ()) { } System.out.println ("threadname=" + thread.currentthread (). GetName () + ", End");} }
public class Run {public static void Main (string[] args) throws interruptedexception { Threadgroup group = new Th Readgroup ("Mythreadgroup"); for (int i=0; i<3; i++) { MyThread thread = new MyThread (Group, "thread" + (i+1)); Thread.Start (); } Thread.Sleep (+); Group.interrupt (); SYSTEM.OUT.PRINTLN ("Call the Interrupt Method");} }
Threadname= thread 1, ready to start a dead loop. Threadname= thread 3, ready to start a dead loop. Threadname= thread 2, ready to start a dead loop. Called the Interrupt method Threadname= thread 3, it's over. Threadname= Thread 1, it's over. Threadname= thread 2, it's over. |
7. Recursive and non-recursive acquisition of objects within the group
public class Run {public static void main (string[] args) throws Interruptedexception {Threadgroup Maingroup = Thread.CurrentThread (). Getthreadgroup (); Threadgroup Groupa = new Threadgroup (Maingroup, "AA"); Runnable Runnable = new Runnable () {public void run () {try {System.out.pri Ntln ("runmethod!"); Thread.Sleep (1000); } catch (Interruptedexception e) {e.printstacktrace (); } } }; Threadgroup GroupB = new Threadgroup (Groupa, "BB"); Allocate space, but not necessarily all threadgroup[] ListGroup1 = new Threadgroup[thread.currentthread (). Getthreadgroup (). Activegroupcount ()]; Incoming true is a recursive get child group and descendant Group Thread.CurrentThread (). Getthreadgroup (). Enumerate (ListGroup1, true); for (int i=0; i<listgroup1.length; i++) {if (listgroup1[i]! = null) {System.out.println ("--" +listgroup1[i].gEtname ()); }} threadgroup[] listGroup2 = new Threadgroup[thread.currentthread (). Getthreadgroup (). Activ Egroupcount ()]; Thread.CurrentThread (). Getthreadgroup (). Enumerate (listGroup2, false); for (int i=0; i<listgroup2.length; i++) {if (listgroup2[i]! = null) {SYSTEM.OUT.PRINTLN ("* *" +listgroup2[i].getname ()); } } }}
Java multithreaded Programming 7--supplements additions--thread groups