Java Programming Ideas Reading notes----The control of the thread Group __ programming

Source: Internet
Author: User
Tags deprecated
Control of thread groups
Aside from security issues, one of the most useful parts of a thread group is control: it takes only a single command to complete the operation of the entire thread group. Under
This example illustrates this and gives a description of the precedence limits within the thread group. The number of annotations in parentheses makes it easier to compare output knots
Fruit:
: Threadgroup1.java
How to thread groups control priorities
of the threads inside them.
public class ThreadGroup1 {
public static void Main (string[] args) {
Get the system thread & print its Info:
Threadgroup sys =
Thread.CurrentThread (). Getthreadgroup ();
Sys.list (); (1)
Reduce the system thread group priority:
Sys.setmaxpriority (thread.max_priority-1);
Increase the main thread priority:
Thread Curr = Thread.CurrentThread ();
Curr.setpriority (curr.getpriority () + 1);
Sys.list (); (2)
Attempt to set a new group to the Max:
Threadgroup G1 = new Threadgroup ("G1");
G1.setmaxpriority (thread.max_priority);
Attempt to set a new thread to the Max:
Thread t = new Thread (G1, "A");
T.setpriority (thread.max_priority);
G1.list (); (3)
Reduce G1 ' s Max priority, then attempt
To increase it:
G1.setmaxpriority (thread.max_priority-2);
G1.setmaxpriority (thread.max_priority);
G1.list (); (4)
Attempt to set a new thread to the Max:
t = new Thread (G1, "B");
T.setpriority (thread.max_priority);
G1.list (); (5)
Lower the max priority below the default
Thread Priority:
G1.setmaxpriority (thread.min_priority + 2);
Look at a new thread ' s priority before
And after changing it:
t = new Thread (G1, "C");
G1.list (); (6)
T.setpriority (T.getpriority ()-1);
G1.list (); (7)
Make G2 a child threadgroup of G1 and
Try to increase its priority:
Threadgroup g2 = new Threadgroup (G1, "G2");
G2.list (); (8)
G2.setmaxpriority (thread.max_priority);
528
G2.list (); (9)
Add a bunch of new threads to G2:
for (int i = 0; i < 5; i++)
New Thread (G2, integer.tostring (i));
Show information about all threadgroups
and Threads:
Sys.list (); (10)
System.out.println ("Starting All Threads:");
thread[] All = new Thread[sys.activecount ()];
Sys.enumerate (All);
for (int i = 0; i < all.length; i++)
if (!all[i].isalive ())
All[i].start ();
Suspends & Stops all threads in
This group and its subgroups:
System.out.println ("All threads Started");
Sys.suspend (); Deprecated in Java 1.2
Never gets here ...
System.out.println ("All Threads suspended");
Sys.stop (); Deprecated in Java 1.2
System.out.println ("All Threads Stopped");
}
} ///:~
The following output has been properly edited so that one page can be mounted (java.lang. has been deleted) and the appropriate number added
Word, which corresponds to the number in parentheses in the previous list of programs:
(1) threadgroup[name=system,maxpri=10]
Thread[main,5,system]
(2) threadgroup[name=system,maxpri=9]
Thread[main,6,system]
(3) threadgroup[name=g1,maxpri=9]
THREAD[A,9,G1]
(4) Threadgroup[name=g1,maxpri=8]
THREAD[A,9,G1]
(5) Threadgroup[name=g1,maxpri=8]
THREAD[A,9,G1]
THREAD[B,8,G1]
(6) Threadgroup[name=g1,maxpri=3]
THREAD[A,9,G1]
THREAD[B,8,G1]
THREAD[C,6,G1]
(7) Threadgroup[name=g1,maxpri=3]
THREAD[A,9,G1]
THREAD[B,8,G1]
THREAD[C,3,G1]
(8) Threadgroup[name=g2,maxpri=3]
(9) Threadgroup[name=g2,maxpri=3]
(10) THREADGROUP[NAME=SYSTEM,MAXPRI=9]
Thread[main,6,system]
THREADGROUP[NAME=G1,MAXPRI=3]
THREAD[A,9,G1]
529
THREAD[B,8,G1]
THREAD[C,3,G1]
THREADGROUP[NAME=G2,MAXPRI=3]
THREAD[0,6,G2]
THREAD[1,6,G2]
THREAD[2,6,G2]
THREAD[3,6,G2]
THREAD[4,6,G2]
Starting All Threads:
All threads started
All programs have at least one thread running, and the first action taken by main () is to call a static (static) of thread
method, named CurrentThread (). Starting with this thread, the thread group is created and the list () is called for the result. The output is as follows:
(1) threadgroup[name=system,maxpri=10]
Thread[main,5,system]
As we can see, the main thread group's name is System, and the main thread's name is main, and it belongs to the system thread group.
The second exercise shows that the highest priority of the system group can be reduced, and the main thread can increase its priority:
(2) threadgroup[name=system,maxpri=9]
Thread[main,6,system]
The third exercise creates a new thread group named G1, which automatically belongs to the system thread group because it does not explicitly specify its attribution
System. We placed a new thread inside the G1, named A. We then try to set the maximum priority of this group to the highest level, and
The priority of A is also set to the highest level. The results are as follows:
(3) threadgroup[name=g1,maxpri=9]
THREAD[A,9,G1]
As you can see, it is not possible to set the maximum priority of a thread group above its parent thread group.
The fourth exercise will reduce the maximum priority of G1 by two, and then try to lift it to thread.max_priority. The results are as follows:
(4) Threadgroup[name=g1,maxpri=8]
THREAD[A,9,G1]
It can also be seen that the attempt to increase the maximum priority is a failure. We can only reduce the maximum priority of one thread group and not improve it.
Also, note that the priority of thread A does not change, and that it is now higher than the maximum priority of the thread group. That is, the thread group's maximum priority
Changes do not affect existing threads.
The fifth exercise tries to Cheng a new line as the maximum priority. As shown below:
(5) Threadgroup[name=g1,maxpri=8]
THREAD[A,9,G1]
THREAD[B,8,G1]
Therefore, the new thread cannot be changed to a level higher than the maximum thread group priority.
The default thread priority for this program is 6; If you create a new thread, that is its default priority and does not change unless you
The first stage is handled in a special order. Practice VI will drop the maximum priority of the thread group below the default thread priority to see if the new
What happens to a thread:
(6) Threadgroup[name=g1,maxpri=3]
THREAD[A,9,G1]
530
THREAD[B,8,G1]
THREAD[C,6,G1]
Although the thread group now has a maximum priority of 3, it still creates a new thread with the default priority 6来. Therefore, the maximum priority of the thread group does not
Affects the default priority (in fact, there seems to be no way to set the default priority for a new thread).
After you have changed the priority, try to lower it one level, and the results are as follows:
(7) Threadgroup[name=g1,maxpri=3]
THREAD[A,9,G1]
THREAD[B,8,G1]
THREAD[C,3,G1]
Therefore, the limit on the maximum priority of the thread group is enforced only when an attempt is made to change the priority level.
We conducted a similar experiment in (8) and (9). Here, we create a new thread group called G2, which takes it as a G1
Subgroup and changed its maximum precedence. As you can see, the priority of G2 cannot be higher than G1 at any rate:
(8) Threadgroup[name=g2,maxpri=3]
(9) Threadgroup[name=g2,maxpri=3]
Also note that when G2 is created, it is automatically set to the maximum priority of the G1 thread group.
After all these experiments, the entire thread group and the threading system are printed, as follows:
(10) THREADGROUP[NAME=SYSTEM,MAXPRI=9]
Thread[main,6,system]
THREADGROUP[NAME=G1,MAXPRI=3]
THREAD[A,9,G1]
THREAD[B,8,G1]
THREAD[C,3,G1]
THREADGROUP[NAME=G2,MAXPRI=3]
THREAD[0,6,G2]
THREAD[1,6,G2]
THREAD[2,6,G2]
THREAD[3,6,G2]
THREAD[4,6,G2]
Therefore, the rules of a thread group limit the maximum priority of a subgroup to at any time be less than or equal to the maximum priority of its parent group.
The last part of this program shows the method for the entire set of threads. The program first traverses the entire thread tree and starts every line that has not yet started
Ride. For example, the system group is then suspended (paused) and finally aborted (although the entire thread group is suspend () and stop ()
Doing so may seem interesting, but it should be noted that these methods are "objected" to in Java 1.2. But while the system group is suspended,
The main thread is also suspended, and the entire program is closed. So it never reaches the point where the thread stops. In fact, if the real
Stop the main thread, it will "throw" a threaddeath violation, so we usually do not do so. Since Threadgroup is from
Object, which contains the wait () method, so you can also call wait (number of seconds x1000) so that when the program pauses to run any number of seconds
Between Of course, the object lock must be acquired in a synchronized block beforehand.
The Threadgroup class also provides the suspend () and resume () methods so that the entire thread group and all its threads can be aborted and started, as well as
Abort and start its subgroups, all with just one command (again, suspend () and resume () are all Java 1.2 "anti-
right).
On the surface, thread groups seem to be a bit confusing, but note that we rarely need to use them directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.