08-02.html
来自「master java threads」· HTML 代码 · 共 654 行 · 第 1/2 页
HTML
654 行
</tr></table> <!-- BEGIN TEXT LINKS --><!-- END TEXT LINKS --><!-- END LEFT NAVBAR --><!-- END LEFT NAVBAR --> <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="08-01.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="08-03.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Priorities</B></FONT></P>
<TABLE WIDTH="100%" BORDER>
<TR>
<CAPTION ALIGN="CENTER" VALIGN="BOTTOM"><B>Table 8-3: Priorities</B></CAPTION>
</TR>
<TR>
<TH VALIGN="TOP" WIDTH="40%" ALIGN="CENTER">Priority</TH>
<TH VALIGN="TOP" WIDTH="60%" ALIGN="CENTER">Description</TH>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>getMaxPriority()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Gets the maximum priority of the group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>setMaxPriority(int)</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Sets the maximum priority of the group.</TD>
</TR>
</TABLE>
<P><FONT SIZE="+1"><B>Daemon Methods</B></FONT></P>
<TABLE WIDTH="100%" BORDER>
<TR>
<CAPTION ALIGN="CENTER" VALIGN="BOTTOM"><B>Table 8-4: Daemon methods</B></CAPTION>
</TR>
<TR>
<TH VALIGN="TOP" WIDTH="40%" ALIGN="CENTER">Method</TH>
<TH VALIGN="TOP" WIDTH="60%" ALIGN="CENTER">Description</TH>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>isDaemon()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Returns the daemon flag of the Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>setDaemon(boolean)</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Changes the daemon status of this group.</TD>
</TR>
</TABLE>
<P><FONT SIZE="+1"><B>Other Methods</B></FONT></P>
<TABLE WIDTH="100%" BORDER>
<TR>
<CAPTION ALIGN="CENTER" VALIGN="BOTTOM"><B>Table 8-5: Other 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>checkAccesso()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Checks to see if the current Thread is allowed to modify this group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>getName()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Gets the name of this Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>getParent()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Gets the parent of this Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>parentOf(Thread Group)</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Checks to see if this Thread group is a parent of or is equal to another Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>toString()</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Returns a String representation of the Thread group.</TD>
</TR>
<TR>
<TD VALIGN="TOP" ALIGN="CENTER"><B>uncaughtException(Thread, Throwable)</B></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Called when a thread in this group exists because of an uncaught exception.</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Operating on an Entire Group of Threads</FONT></H4>
<P>The following four methods operate on all of the threads in a specified ThreadGroup:
</P>
<DL>
<DD><B>•</B> <B>destroy()</B>
<DD><B>•</B> <B>resume()</B>
<DD><B>•</B> <B>stop()</B>
<DD><B>•</B> <B>suspend()</B>
</DL>
<H3><A NAME="Heading11"></A><FONT COLOR="#000077">Limiting Priorities</FONT></H3>
<P>You can limit the priority value of any thread that is newly inserted into a ThreadGroup. The <B>setMaxPriority()</B> method will set a limit of the ThreadGroup as a whole.</P>
<P>For example, note the following code:</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="90%" ALIGN="CENTER">
<TR><TD>
<!-- CODE SNIP //-->
<PRE>
ThreadGroup m_backgroundThreadGroup = new
ThreadGroup(“BackgroundThreads”);
Thread m_bgThread1 = new Thread(m_backgroundThreadGroup, this);
m_backgroundThreadGroup.setMaxPriority(Thread.MIN_PRIORITY+2);
Thread m_bgThread2 = new Thread(mPbackgroundThreadGroup, this);
</PRE>
<!-- END CODE SNIP //-->
</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Changing Priorities</FONT></H4>
<P>The threads <SMALL>m_bgThread1</SMALL> and <SMALL>m_bgThread2</SMALL> are usually created with the default priority of <B>NORMAL_PRIORITY</B>, which is equal to the number 5. However, before the thread <SMALL>m_bgThread2</SMALL> was created, the maximum priority of the ThreadGroup was set to <B>MIN_PRIORITY+2</B>, which is equal to the number 3. Thus, in this particular case, when <SMALL>m_bgThread2</SMALL> was created, it actually received a priority value of 3.</P>
<P>Threads that are created before the call to <B>setMaxPriority()</B> will retain their old priority. Thus, in the case of the thread <SMALL>m_bgThread1</SMALL>, it still retains the priority value of 5 even though the max priority of the ThreadGroup was changed to 3.</P>
<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">Priorities Characteristics</FONT></H4>
<P>Note the following characteristics of Java priorities:
</P>
<DL>
<DD><B>•</B> <B>setMaxPriority()</B> will change the max priority value of all ThreadGroups which are descendants of a ThreadGroup.
<DD><B>•</B> If you attempt to set the priority of a thread higher than the priority of the ThreadGroup to which it belongs, a SecurityException will be thrown.
<DD><B>•</B> Once the maximum priority of a ThreadGroup has been lowered, it cannot be raised again.
<DD><B>•</B> The maximum priority of the system ThreadGroup is <B>MAX-PRIORITY (10)</B>. The maximum priority of the applet ThreadGroup is 6.
</DL>
<TABLE WIDTH="90%">
<TR>
<TD VALIGN="TOP" WIDTH="5%" ALIGN="LEFT"><IMG SRC="images/08-03i.jpg"></TD>
<TD VALIGN="TOP" ALIGN="LEFT">The <B>setDaemon()</B> function will change the daemon status of the ThreadGroup. It will not alter the daemon status of any individual thread within the group.<BR><BR>If you make the group a daemon group, it will be destroyed automatically when all of the threads it contains are terminated.</TD>
</TR>
</TABLE>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Java Security & checkAccess[] Method</FONT></H3>
<P>Most users of the Internet—and Java applets in particular—are concerned about security. You would not want a Java applet from one Internet server illegally or maliciously modifying the ThreadGroups or threads of an applet on another Internet server. To this end, there exists some access checking regarding threads and ThreadGroups.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">checkAccess[?] Method</FONT></H4>
<P>The Thread and ThreadGroup classes both have a method called <B>checkAccesso()</B>. They both call the Security Manager’s <B>checkAccess()</B> method. The Security Manager checks to see if the threads are allowed access to certain operations. If they are, the Security Manager simply returns. If they are not, the Security Manager throws a SecurityException.</P>
<DL>
<DD><B>•</B> <B>void checkAccess(Thread t)</B>: checks if the current thread is allowed to modify the state of thread.
<DD><B>•</B> <B>void checkAccess(ThreadGroup g)</B>: checks if the current ThreadGroup is allowed to modify the state of ThreadGroup g.
</DL>
<P>Internally, most of the methods of class Thread and of class ThreadGroup call the Security Manager before performing the actual operation. For example, if you examine the implementation of the <B>stop()</B> method of the Thread class, you will find the following</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="90%" ALIGN="CENTER">
<TR><TD>
<!-- CODE SNIP //-->
<PRE>
public void stop()
{
SecurityManager s = System.getSecurityManagero();
If (s != null)
s.checkAccess(this);
stop0(new ThreadDeatho());
}
</PRE>
<!-- END CODE SNIP //-->
</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">All-of-none Security</FONT></H4>
<P>The Java SecurityManager implements security on an “all-of-none” basis. There is no granularity to distinguish allowable operations. In other words, if the SecurityManager does not allow a thread to suspend another thread, then it also does not allow the same thread to set the other thread’s daemon status.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">Calling ThreadGroup’s checkAccess[] Method</FONT></H4>
<P>The following is a list of ThreadGroup methods that call the ThreadGroup’s <B>checkAccesso()</B> method:</P>
<DL>
<DD><B>•</B> <B>ThreadGroup(ThreadGroup parent, String name)</B>
<DD><B>•</B> <B>setDaemon</B>
<DD><B>•</B> <B>setMaxPriority</B>
<DD><B>•</B> <B>stop</B>
<DD><B>•</B> <B>suspend</B>
<DD><B>•</B> <B>resume</B>
<DD><B>•</B> <B>destroy</B>
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Calling checkAccess[]</FONT></H4>
<P>The following Thread methods call <B>checkAccess():</B></P>
<DL>
<DD><B>•</B> constructors that specify a thread group
<DD><B>•</B> <B>stop</B>
<DD><B>•</B> <B>suspend</B>
<DD><B>•</B> <B>resume</B>
<DD><B>•</B> <B>setPriority</B>
<DD><B>•</B> <B>setName</B>
<DD><B>•</B> <B>setDaemon</B>
</DL>
<P>A stand-alone Java app has no security manager, so threads can modify and inspect any other thread.
</P>
<TABLE WIDTH="90%"><TR>
<TD VALIGN="TOP" WIDTH="5%" ALIGN="LEFT"><IMG SRC="images/08-04i.jpg"></TD>
<TD VALIGN="TOP" ALIGN="LEFT">Within an Applet, a thread can manipulate another thread only if both threads are members of the Applet’s ThreadGroup. A thread cannot manipulate a thread that is in another applet.</TD>
</TR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="08-01.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="08-03.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 -->
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?