📄 tij0158.html
字号:
the listing above.
</FONT><P></DIV>
<font color="#990000"><PRE>(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]
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 </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
programs have at least one thread running, and the first action in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is to call the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Thread</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>currentThread( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
From this thread, the thread group is produced and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is called for the result. The output is:
</FONT><P></DIV>
<font color="#990000"><PRE>(1) ThreadGroup[name=system,maxpri=10]
Thread[main,5,system] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the name of the main thread group is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>system</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and the name of the main thread is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and it belongs to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>system</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
thread group.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second exercise shows that the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>system</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
group’s maximum priority can be reduced and the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
thread can have its priority increased:
</FONT><P></DIV>
<font color="#990000"><PRE>(2) ThreadGroup[name=system,maxpri=9]
Thread[main,6,system] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
third exercise creates a new thread group,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which automatically belongs to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>system</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
thread group since it isn’t otherwise specified. A new thread
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
placed in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
After attempting to set this group’s maximum priority to the highest
level and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’s
priority to the highest level, the result is:
</FONT><P></DIV>
<font color="#990000"><PRE>(3) ThreadGroup[name=g1,maxpri=9]
Thread[A,9,g1] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Thus,
it’s not possible to change the thread group’s maximum priority to
be higher than its parent thread group.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
fourth exercise reduces
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’s
maximum priority by two and then tries to increase it up to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Thread.MAX_PRIORITY</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The result is:
</FONT><P></DIV>
<font color="#990000"><PRE>(4) ThreadGroup[name=g1,maxpri=8]
Thread[A,9,g1] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the increase in maximum priority didn’t work. You can only
decrease a thread group’s maximum priority, not increase it. Also, notice
that thread
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’s
priority didn’t change, and now it is higher than the thread
group’s maximum priority. Changing a thread group’s maximum
priority doesn’t affect existing threads.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
fifth exercise attempts to set a new thread to maximum priority:
</FONT><P></DIV>
<font color="#990000"><PRE>(5) ThreadGroup[name=g1,maxpri=8]
Thread[A,9,g1]
Thread[B,8,g1] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
new thread cannot be changed to anything higher than the maximum thread group
priority.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
default thread priority for this program is 6; that’s the priority a new
thread will be created at and where it will stay if you don’t manipulate
the priority. Exercise six lowers the maximum thread group priority below the
default thread priority to see what happens when you create a new thread under
this condition:
</FONT><P></DIV>
<font color="#990000"><PRE>(6) ThreadGroup[name=g1,maxpri=3]
Thread[A,9,g1]
Thread[B,8,g1]
Thread[C,6,g1] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Even
though the maximum priority of the thread group is 3, the new thread is still
created using the default priority of 6. Thus, maximum thread group priority
does not affect default priority. (In fact, there appears to be no way to set
the default priority for new threads.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">After
changing the priority, attempting to decrement it by one, the result is:
</FONT><P></DIV>
<font color="#990000"><PRE>(7) ThreadGroup[name=g1,maxpri=3]
Thread[A,9,g1]
Thread[B,8,g1]
Thread[C,3,g1] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Only
when you attempt to change the priority is the thread group’s maximum
priority enforced.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
similar experiment is performed in (8) and (9), in which a new thread group
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g2
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
created as a child of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and its maximum priority is changed. You can see that it’s impossible for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’s
maximum to go higher than
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’s:</FONT><P></DIV>
<font color="#990000"><PRE>(8) ThreadGroup[name=g2,maxpri=3]
(9) ThreadGroup[name=g2,maxpri=3] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Also
notice that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is automatically set to the thread group maximum priority of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">After
all of these experiments, the entire system of thread groups and threads is
printed out:
</FONT><P></DIV>
<font color="#990000"><PRE>(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] </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">So
because of the rules of thread groups, a child group must always have a maximum
priority that’s less than or equal to its parent’s maximum priority.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
last part of this program demonstrates methods for an entire group of threads.
First the program moves through the entire tree of threads and starts each one
that hasn’t been started. For drama, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>system</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
group is then suspended and finally stopped. (Although it’s interesting
to see that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>suspend( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stop( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
work on entire thread groups, you should keep in mind that these methods are
deprecated in Java 1.2.<A NAME="Index2602"></A>)
But when you suspend the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>system</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
group you also suspend the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
thread and the whole program shuts down, so it never gets to the point where
the threads are stopped. Actually, if you do stop the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
thread it throws a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ThreadDeath</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
exception, so this is not a typical thing to do. Since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ThreadGroup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is inherited from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which contains the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>wait( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, you can also choose to suspend the program for any number of seconds by
calling
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>wait(seconds
* 1000)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This must acquire the lock inside a synchronized block, of course.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ThreadGroup</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class also has
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>suspend( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>resume( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods so you can stop and start an entire thread group and all of its threads
and subgroups with a single command. (Again,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>suspend( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>resume( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are deprecated in Java 1.2.<A NAME="Index2603"></A>)</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Thread
groups can seem a bit mysterious at first, but keep in mind that you probably
won’t be using them directly very often.
</FONT><a name="_Toc375545486"></a><a name="_Toc408018760"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn62" HREF="#fnB62">[62]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><I>The
Java Programming Language
</I></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">,
by Ken Arnold and James Gosling, Addison-Wesley 1996 pp 179.
</FONT><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0157.html">Prev</a> | <a href="tij0159.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -