?? 08-01.html
字號(hào):
<td width="15"><img src="/images/dotclear.gif" width="15" alt="" border="0"></td><!-- end of ITK left NAV --><!-- begin main content --> <td width="100%" valign="top" align="left"> <br><!-- END SUB HEADER --><!-- Created by dB Page Builder. http://www.pchelponline.com/bluestem -->
<!--Begin Content Column -->
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="/images/sm_covers/1562438425.gif" width=60 height=73 alt="Mastering Java Threads" border="1">
</td>
<td align="left">
<font face="arial, helvetica" size="-1" color="#336633"><b>Mastering Java Threads</b></font>
<br>
<font face="arial, helvetica" size="-1"><i>by Marc Adler and David Herst</i>
<br>
DDC Publishing, Inc.
<br>
<b>ISBN:</b> 1562438425<b> Pub Date:</b> 05/01/99</font> <A HREF="http://www.digitalguru.com/dgstore/product.asp?isbn=1562438425&ac%5Fid=28" TARGET="anotherwindows"><img src="/images/buyit.gif" width=64 height=23 hspace="5" align="middle" alt="Buy It" border="0"></a>
</td>
</tr>
</table>
<P>
<form name="advanced" method="POST" action="http://ewsearch.earthweb.com:80/jsp/k2search/ewintrak2search_p2.jsp" onSubmit=" return checkForQuery(this); ">
<INPUT type="hidden" name="collection" value="corpitk_p2">
<INPUT type="hidden" name="altcoll" value="allbooks_p2">
<INPUT type="hidden" name="hl" value="on">
<INPUT name="sortspec" type=hidden value="score desc">
<INPUT name="fields" type=hidden value="vdkvgwkey score vstitle vsisbn vsauthor vspublisher vspubdate">
<INPUT name="imageprefix" type=hidden value="http://academic.itknowledge.com">
<INPUT name="ssiFolder" type=hidden value="itkaca">
<INPUT name="topics" type=hidden value="itk_academic">
<INPUT type="hidden" name="bookid" value="t_1562438425">
<font face="arial, helvetica" size=2><b>Search this book:</b></font><br>
<INPUT NAME="query" size=25 VALUE=""> <input type="image" width=28 height=23 border=0 value="Go" name="Go" src="/images/go.gif" align=absmiddle>
</form>
<!-- Empty Reference Subhead -->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch07/07-01.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="08-02.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 8<BR>Thread Groups
</FONT></H2>
<P><BIG><B>Lesson Topics</B></BIG></P>
<DL>
<DD><B>•</B> Introduction to ThreadGroups
<DD><B>•</B> ThreadGroup Naming
<DD><B>•</B> The ThreadGroup Class in Detail
<DD><B>•</B> Limiting Priorities
<DD><B>•</B> Java Security & checkAccess( ) Method
<DD><B>•</B> An Example of Programming with ThreadGroups
</DL>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Introduction to ThreadGroups</FONT></H3>
<P><I>ThreadGroup</I> is a class that serves to organize threads. It is analogous to the concept of directories and files within an operating system (such as UNIX or DOS). You can consider a ThreadGroup to be similar to a directory and an individual thread similar to a file.</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">ThreadGroup Characteristics</FONT></H4>
<P>A ThreadGroup can contain a group of threads, or it can contain other ThreadGroups that, in turn, contain threads. This is the same concept of a directory that contains a combination of files and subdirectories. You can accurately envision a ThreadGroup hierarchy as a tree structure.
</P>
<P><FONT SIZE="+1"><B>Global ThreadGroup Operations</B></FONT></P>
<P>Unlike the directory/file analogy, there are certain operations that you can perform on a ThreadGroup that affect every thread that is a descendant of the ThreadGroup. For example, you can—with just one call—start or stop every thread that is in the ThreadGroup.
</P>
<TABLE WIDTH="90%"><TR>
<TD VALIGN="TOP" WIDTH="5%" ALIGN="LEFT"><IMG SRC="images/08-01i.jpg"></TD>
<TD VALIGN="TOP" ALIGN="LEFT">The main reason to use ThreadGroups is to manipulate multiple threads with a single call.</TD>
</TR>
</TABLE>
<P>The following code illustrates how you can write a tree-like hierarchy of ThreadGroups:
</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="90%" ALIGN="CENTER">
<TR><TD>
<!-- CODE SNIP //-->
<PRE>
ThreadGroup g1 = new ThreadGroup (“G1”);
ThreadGroup g2 = new ThreadGroup (g1, “G2”);
ThreadGroup g3 = new ThreadGroup (g1, “G3”);
ThreadGroup g4 = new ThreadGroup (g3, “G4”);
ThreadGroup g5 = new ThreadGroup (“G5”);
</PRE>
<!-- END CODE SNIP //-->
</TD>
</TR>
</TABLE>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/08-01.jpg',176,211)"><IMG SRC="images/08-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/08-01.jpg',176,211)"><FONT COLOR="#000077"><B>Figure 8-1</B></FONT></A> The hierarchy of ThreadGroups</P>
<P>There are several scenarios in which you can use ThreadGroups. A common operation which developers perform on a ThreadGroup is simultaneously stopping all threads. It is easier to do this with a single call rather than cycle through all threads in your application.
</P>
<P>For other examples, think of your stock quote monitoring application. You may want to set up a specific ThreadGroup for all threads that poll different servers for financial news. You might want to lower the priority of all news reading threads if, for example, there is heavy volume on a particular stock that you are monitoring.</P>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">ThreadGroup Naming</FONT></H3>
<P>ThreadGroups are organized by name. The following rules apply to ThreadGroup names:
</P>
<DL>
<DD><B>•</B> each ThreadGroup must have a unique name
<DD><B>•</B> both ThreadGroup constructors take a name as an argument
<DD><B>•</B> the default ThreadGroup in a Java application is called “main”
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">The Main ThreadGroup</FONT></H4>
<P>When your application starts, the main thread is in “main.” Unless otherwise specified, all threads will be created as part of the “main” ThreadGroup.
</P>
<TABLE WIDTH="90%"><TR>
<TD VALIGN="TOP" WIDTH="5%" ALIGN="LEFT"><IMG SRC="images/08-02i.jpg"></TD>
<TD VALIGN="TOP" ALIGN="LEFT">If you run your Applet within a browser, the name of the root ThreadGroup might be something different. This depends on which browser you are running.</TD>
</TR>
</TABLE>
<P>If you create a new thread in your application, and you do not specify a ThreadGroup in the thread’s constructor, the new thread is placed in the same ThreadGroup as its creator.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Thread Constructors</FONT></H4>
<P>The following four thread constructor methods create the thread in the current ThreadGroup (the same ThreadGroup in which is found the new thread’s creator):
</P>
<DL>
<DD><B>•</B> <B>Thread()</B>
<DD><B>•</B> <B>Thread(Runnable)</B>
<DD><B>•</B> <B>Thread(String)</B>
<DD><B>•</B> <B>Thread(Runnable, String)</B>
</DL>
<P>The following three thread constructors create the thread in a specific ThreadGroup:
</P>
<DL>
<DD><B>•</B> <B>Thread(ThreadGroup, Runnable)</B>
<DD><B>•</B> <B>Thread(ThreadGroup, String)</B>
<DD><B>•</B> <B>Thread(ThreadGroup, Runnable, String)</B>
</DL>
<P>You can learn which ThreadGroup to which a particular thread belongs by using the <B>getThreadGroup()</B> method of class Thread, as shown below:</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="90%" ALIGN="CENTER">
<TR><TD>
<!-- CODE SNIP //-->
<PRE>
ThreadGroup g = m_UIThread.getThreadGroup ( );
</PRE>
<!-- END CODE SNIP //-->
</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Enumerating Threads Within a ThreadGroup</FONT></H4>
<P>There are methods that allow you to enumerate threads within a particular ThreadGroup, as shown below:
</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="90%" ALIGN="CENTER">
<TR><TD>
<!-- CODE //-->
<PRE>
ThreadGroup g = Thread.currentThread().getThreadGroup();
int nThreads = g.getActiveCount();
Thread threads[] = new Thread[nThreads];
g.enumerate(threads);
for (int i = 0; i < nThreads; i++)
{
doSomethingToThread(threads[i]);
}
</PRE>
<!-- END CODE //-->
</TD>
</TR>
</TABLE>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">The ThreadGroup Class in Detail</FONT></H3>
<P>The <B>java.lang</B> package contains the implementation of the ThreadGroup class. The declaration of this class is as follows:</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="90%" ALIGN="CENTER">
<TR><TD>
<!-- CODE SNIP //-->
<PRE>
public class ThreadGroup extends Object
</PRE>
<!-- END CODE SNIP //-->
</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Methods</FONT></H4>
<P>The following constructor methods are available for the ThreadGroup class:
</P>
<DL>
<DD><B>•</B> <B>ThreadGroup(String):</B> creates a new ThreadGroup
<DD><B>•</B> <B>ThreadGroup(ThreadGroup, String):</B> creates a new ThreadGroup with a specified name in the specified Thread group
</DL>
<P>Table 8-1 lists methods which query and change the state of Threads within a ThreadGroup.
</P>
<TABLE WIDTH="100%" BORDER><TR>
<CAPTION ALIGN="CENTER" VALIGN="BOTTOM"><B>Table 8-1: Methods which query and change the state of Threads</B></CAPTION>
</TR>
<TR>
<TH VALIGN="TOP" WIDTH="30%" ALIGN="CENTER">Method</TH>
<TH VALIGN="TOP" WIDTH="70%" ALIGN="CENTER">Description</TH>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>Destroy()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">• Destroys a Thread group.<BR>• Can only be called if there are no active threads within the group.<BR>• Destroys all descendant ThreadGroups recursively.<BR>• If any of the ThreadGroups have an active thread, a IllegalThreadStateException will be thrown.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>Resume()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Resumes all the Threads in this Thread group and all of its sub groups.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>stop()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Stops all the Threads in this Thread group and all of its sub groups.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>Suspend()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Suspends all the Threads in this Thread group and all of its sub groups.</TD>
</TR>
</TABLE>
<P><FONT SIZE="+1"><B>Informational and Enumeration Methods</B></FONT></P>
<TABLE WIDTH="100%" BORDER>
<TR>
<CAPTION ALIGN="CENTER" VALIGN="BOTTOM"><B>Table 8-2: Information & enumeration methods</B></CAPTION>
</TR>
<TR>
<TH VALIGN="TOP" WIDTH="30%" ALIGN="CENTER">Method</TH>
<TH VALIGN="TOP" WIDTH="70%" ALIGN="CENTER">Description</TH>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>ActiveCount()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Returns an estimate of the number of active Threads in the Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>ActiveGroupCount()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Returns an estimate of the number of active groups in the Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>enumerate(Thread[])</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Copies, into the specified array, references to every active Thread in this Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>enumerate(Thread[], boolean)</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Copies, into the specified array, references to every active Thread in this Thread group. The boolean is true if the ThreadGroup tree should be enumerated recursively.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>enumerate(ThreadGroup[])</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Copies, into the specified array, references to every active Thread group in this Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>enumerate(ThreadGroup[], boolean)</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Copies, into the specified array, references to every active Thread group in this Thread group. The boolean is true if the ThreadGroup tree should be enumerated recursively.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>list()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Lists this Thread group.</TD>
</TR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch07/07-01.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="08-02.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/ddc00001.html">DDC Publishing</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --><!-- BEGIN SUB FOOTER --> <br> <img src="/images/dotclear.gif" width="5" height="7" border="0"> </TD> </TR> </TABLE> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="left" width="160"><img src="/images/bot_curve.jpg" width="160" alt="" border="0"></td> <td align="left" valign="top" nowrap><a href="/"><img src="/images/top_tabs/home_bot.gif" alt="home" border="0"></a><!-- <a href="/content/corp.html"><img src="/images/top_tabs/subscribe_bot.gif" alt="Subscribe" border="0"></a> --><a href="/search/"><img src="/images/top_tabs/search_bot.gif" alt="search" border="0"></a><a href="/faq/faq.html"><img src="/images/top_tabs/faq_bot.gif" alt="faq" border="0"></a><a href="/sitemap.html"><img src="/images/top_tabs/sitemap_bot.gif" alt="sitemap" border="0"></a><a href="/contactus.html"><img src="/images/top_tabs/contact_us_bot.gif" alt="contactus" border="0"></a><img src="/images/dotclear.gif" width=260 height="1" alt="" border="0"></td> </tr></table> <table width="100%" bgcolor="#003366" border=0 cellpadding=0 cellspacing=0> <tr> <td align="left" width=145><img src="/images/dotclear.gif" width=145 height="1" alt="" border="0"></td> <!-- END SUB FOOTER -->
<!-- all of the books have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --><!-- FOOTER --> <td align="left" bgcolor="#003366"><table border="0" cellspacing="10" cellpadding="5"><tr><td align="center"><font face="arial, helvetica" size="1" color="#cccccc"><b><a href="/products.html"><font color="#0099CC">Products</font></a> | <a href="/contactus.html"><font color="#0099CC">Contact Us</font></a> | <a href="http://www.earthweb.com/dlink.corp|about_us-jhtml.72.0.-.0.jhtml" target="resource window"><font color="#0099CC">About Us</font></a> | <a href="http://www.earthweb.com/dlink.corp|privacy-jhtml.72.0.-.-.jhtml" target="resource window"><font color="#0099CC">Privacy</font></a> | <a href="http://www.itmarketer.com/" target="resource window"><font color="#0099CC">Ad Info</font></a> | <!--<a href="/consortia/"><font color="#0099CC">Consortia</font></a> | --><a href="/"><font color="#0099CC">Home</font></a></b><br><br>Use of this site is subject to certain <a href="/agreement.html"><font color="#0099CC">Terms & Conditions</font></a>, <a href="/copyright.html"><font color="#0099CC">Copyright © 1996-2000 EarthWeb Inc.</font></a> All rights reserved. Reproduction in whole or in part in any form or medium without express written <a href="http://www.earthweb.com/dlink.corp|permissions-jhtml.72.0.-.-.jhtml" target="resource window"><font color="#0099CC">permission</font></a> of EarthWeb is prohibited. Read EarthWeb's <A HREF="http://www.earthweb.com/dlink.corp|privacy-jhtml.72.0.-.-.jhtml" target="resource window"><font color="#0099CC">privacy</font></A> statement.</font><br><br></td></tr></table><a href="AITK1a2b3c4d5e6f7g8h9idefcon4.html"><img src="/images/dotclear.gif" border="0" height="1" width="1" align="left"></a></td> </tr></table><!--DoubleClick Ad BEGIN--><SCRIPT LANGUAGE="JavaScript"><!--document.write('<layer src="http://ad.doubleclick.net/adl/academic.itknowledge.com/homepage;cat=homepage;cat=enterprise;cat=education;cat=it_training;ord=' + ord + '" width="468" height="60" visibility="hide" onload="moveToAbsolute(ph1.pageX, ph1.pageY); visibility=\'show\';" clip="468,60"></layer>');document.write('<LAYER SRC="http://ad.doubleclick.net/adl/itkaca.earthweb.dart/b_aca_soft_dev;a=b_aca_soft_dev4;sz=160x60;ord=' + ord + '" width=160 height=60 visibility="hidden" onLoad="moveToAbsolute(layer1.pageX,layer1.pageY);clip.height=60;clip.width=160; visibility=\'show\';"></LAYER>');//--></SCRIPT> <!--DoubleClick Ad END--></BODY></HTML><!-- END FOOTER -->
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -