Thread Group
Threads can be attributed to a thread group, thread groups can be wired to the object, can also be wired to groups, the group can also be wired, such an organizational structure is somewhat similar to the form of a tree:
The role of a thread group is to manage threads or thread group objects in bulk, effectively organizing threads or thread group objects .
Thread Affinity Thread Group: Level 1 Association
A 1-level association is a child object in a parent object, but does not create a grandchild object. This situation is common in development, such as when creating threads, in order to effectively block management of these threads, typically creating a thread group and then attributing some of the threads to the group to effectively manage the scattered thread objects.
Take a look at the simple 1-level correlation example:
Implements runnable{ voidtry while(! Thread.CurrentThread (). isinterrupted ()) {System.out.println ("ThreadName =" + thread.currentthread (). GetName () ); Thread.Sleep ( interruptedexception e) {e.printstacktrace (); }}}
public static Void main (string[] args) {MyThread49 mt0 = new MyThread49 (); MyThread49 MT1 = new MyThread49 (); Threadgroup TG = new threadgroup ("New Thread Group 1" ); Thread T0 = new thread (TG, mt0); Thread T1 = new thread (TG, MT1); T0.start (); T1.start (); System.out.println ("Number of threads active:" + Tg.activecount ()); System.out.println (the name of the thread group is: "+ Tg.getname ())}
Look at the results of the operation:
The number of threads active is: 2threadname = Thread-1threadname = Thread-0 The name of the thread group is: new Thread Group 1ThreadName = Thread-1threadname = Thread-0threadname = Thread-1threadname = Thread-0threadname = Thread-1threadname = Thread-0
...
The information printed on 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 are printed in an infinite 3-second interval, which also conforms to the code expectations
Thread Affinity Thread Group: Multilevel Association
The so-called multilevel Association is that there are child objects in the parent object, and then child objects are created in the sub-object to buy will also appear descendants of the effect. However, this type of writing is less common in development, because the threading tree, if involved, is not conducive to the management of thread objects, but the JDK does provide a multi-level associative thread tree structure.
Multi-level associated code is not written, simply look at how to use the Shutdown Association, see the next JDK API Threadgroup construction method:
Note the second, if you want to use a multilevel association is usually the second constructor. The first parameter represents the parent thread group of the new Thread group, the second parameter represents the name of the new thread group, the parent thread group and the new thread group name, and a new thread group can naturally be constructed.
Of course it is possible to use the first construction method, which is mentioned in the next section.
Also note that the thread must be started before it can be classified into the specified thread group .
Thread Group Automatic attribution feature
Automatic attribution means auto-attribution to the current thread group, and take a look at the example:
PublicStaticvoidMain (string[] args) {System.out.println ("a thread:" +Thread.CurrentThread (). GetName () + ", owning thread:" +Thread.CurrentThread (). Getthreadgroup (). GetName () + ", number of threads in group:" + Thread.CurrentThread (). Getthreadgroup (). Activegroupcount ()); Threadgroup Group = new Threadgroup ("new Group" ); SYSTEM.OUT.PRINTLN ("B Thread:" + Thread.CurrentThread ()." GetName () + ", owning thread:" + Thread.CurrentThread (). Getthreadgroup (). GetName () + ", number of threads in group:" + Thread.CurrentThread (). Getthreadgroup (). Activegroupcount ()); threadgroup[] TG = new threadgroup[ Thread.CurrentThread (). Getthreadgroup (). Activegroupcount (); Thread.CurrentThread (). Getthreadgroup (). Enumerate (TG); for (int i = 0; i < tg.length; I++) System.out.println ("First Thread group name:" + Tg[i].getname ())}
Look at the results of the operation:
A thread: Main, owning thread: Main, number of threads in group: 0B Thread: Main, owning thread: Main, number of threads group in group: 1 The first thread group name is: New Group
From the result, instantiating a group, without specifying a thread group, automatically falls into the group of threads to which the current thread belongs , implicitly adding a group of sub-threads to a thread group.
Root Thread Group
Take a look at the root thread group:
void Main (string[] args) { System.out.println (Thread.CurrentThread (). Getthreadgroup (). GetParent (). GetName ()); System.out.println (Thread.CurrentThread (). Getthreadgroup (). GetParent (). GetParent (). GetName ());}
Look at the results of the operation:
SystemException in thread "main" java.lang.NullPointerException at Com.xrq.example.e49.TestMain49.main ( Testmain49.java:11)
The results of the operation can be reached by two conclusions:
1. The root thread group is the system thread Group systems
2. The null pointer exception is because there are no thread groups on the system thread group, so the GetParent () method of the systems returns NULL, and a null call to the GetName () method is naturally NullPointerException
For the root thread group, take a look at the source code of Threadgroup:
/** * Creates an empty thread group, which is the not on any Thread group. */Private Threadgroup () { // called from C codethis.name = "System"; this.maxpriority = thread.max_priority;}
A private construction method, stating that it is not open to developers. The comment is already written clearly, which is called by C code and is used to build the system thread group.
Bulk Stop Group Inline
Using thread groups is a natural way to do batch management of threads, so far we don't seem to see how to do bulk operations on threads within a thread group, and finally look at the threads in the bulk operations thread Group:
Extends thread{public Supervoid run () {System.out.println ("ThreadName =" + Thread.CurrentThread (). GetName () + "Ready to start Dead loop" while(! this. isinterrupted ()) {} System.out.println ("ThreadName =" + Thread.CurrentThread (). GetName () + "End");}}
Open 3 Threads:
Throws interruptedexception{ new Threadgroup ("My thread group"NULL for(int i = 0; i < 3; i++new My THREAD50 (TG, "thread" + i); Mt.start ();} Thread.Sleep (tg.interrupt); System.out.println ("called the Interrupt () method");}
Look at the results of the operation:
ThreadName = Thread 0 ready to start a dead loop ThreadName = Thread 2 ready to start a dead loop ThreadName = Thread 1 ready to start a dead loop called the Interrupt () method threadname = Thread 2 ended ThreadName = thread 1 ended threadname = thread 0 Ended
It is threadgroup to see that the interrupt () method in Threadgroup has been called to break threads in the thread group in bulk. Additional thread group operations can be viewed in the JDK API.
Java Multi-Threading 16: Thread Groups