05-01.html
来自「master java threads」· HTML 代码 · 共 594 行 · 第 1/2 页
HTML
594 行
<font face="Arial,helvetica" size="1"> <SELECT NAME="cat" onChange='top.location.href=this.options[selectedIndex].value;' style="font-size: 10; font-family: sans-serif;"> <option value="" selected>Please Select <option value="">----------- <option value="/reference/dir.1-busman.html">Business & IT Mgmt <option value="/reference/dir.2-certification.html">Certification & Training <option value="/reference/dir.3-databaseanderp.html">Database & ERP <option value="/reference/dir.4-desktopapps.html">Desktop Apps <option value="/reference/dir.5-desktoppubanddesign.html">Graphic Design <option value="/reference/dir.6-generalinternet.html">General Internet <option value="/reference/dir.7-hardwareandos.html">Hardware & OS <option value="/reference/dir.8-ibmredbooks.html">IBM RedBooks <option value="/reference/dir.9-networktelcom.html">Network & Telecom <option value="/reference/dir.10-websoftwaredev.html">Web & Software Dev <option value="/reference/dir.11-careers.html">Careers <option value="">----------- <option value="/reference/whatsnew.html">New Arrivals </SELECT> </font> </td> </tr></table></form> <!-- LEFT NAV SEARCH END --><table border="0" cellspacing="0" cellpadding="0"> <tr> <td><a href="/desktop/"><img src="/images/desktop_icona.gif" border=0 alt="Go to ITKnowledge Academic"></a><br><br></td> </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="../ch04/04-02.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/06-01.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 5<BR>The Life of a Thread
</FONT></H2>
<P><BIG><B>Lesson Topics</B></BIG></P>
<DL>
<DD><B>•</B> Different Thread States
</DL>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Different Thread States</FONT></H3>
<P>A thread can exist in many different states during its “lifetime.” Figure 5-1 displays is a list of the various states in which a thread can exist.
</P>
<DL>
<DD><B>•</B> New thread
<DD><B>•</B> Runnable state
<DD><B>•</B> Not runnable state
<DD><B>•</B> Dead state
</DL>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/05-01.jpg',500,234)"><IMG SRC="images/05-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/05-01.jpg',500,234)"><FONT COLOR="#000077"><B>Figure 5-1</B></FONT></A> Various states of a thread</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">New Thread (New State)</FONT></H4>
<P>A thread must somehow be created in memory. The following code accomplishes thread creation:
</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD>
<!-- CODE SNIP //-->
<PRE>
Thread t = new Thread (“MyThread”);
</PRE>
<!-- END CODE SNIP //-->
</TD>
</TR>
</TABLE>
<P>This code creates an <I>empty</I> thread. There are no system resources allocated to this thread. The thread is in the <I>new thread</I> state. The only operations which can be performed on a new thread are:</P>
<DL>
<DD><B>•</B> to <B>start()</B> it
<DD><B>•</B> to <B>stop()</B> it
</DL>
<P>If you try to do anything else to a new thread, you will generate the following exception:
</P>
<!-- CODE SNIP //-->
<PRE>
IllegalThreadState
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Runnable State</FONT></H4>
<P>When you call a new thread’s <B>start()</B> method, the thread’s state changes from New to Runnable. After a thread is changed from New to Runnable, Java will:</P>
<DL>
<DD><B>•</B> allocate system resources to the thread
<DD><B>•</B> schedule the thread for execution
<DD><B>•</B> allocate stack space for the thread
<DD><B>•</B> call the thread’s <B>run()</B> method
</DL>
<P>The Java scheduler will allocate processor time among the various threads that are in the Runnable state. You can force a thread to relinquish processor time by calling its <B>yield()</B> method. When you call <B>yield()</B>, the thread will remain in the Runnable state. However, it will allow the Java scheduler to run another thread that might be waiting for its chance.</P>
<P><FONT SIZE="+1"><B>Transitions Between States</B></FONT></P>
<P>When a thread is in the Runnable state, it can transition to the Not Runnable state or the Dead state. If you call a thread’s <B>stop()</B> method, it will go to the Dead state. There are various ways in which a thread can go from the Runnable to the Not Runnable state. The ways are outlined on the following page.</P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Not Runnable State</FONT></H4>
<P>Table 5-1 outlines the ways in which a thread can enter and exit a Not Runnable state.
</P>
<TABLE WIDTH="100%" BORDER><TR>
<CAPTION VALIGN="BOTTOM"><B>Table 5-1: Ways in which a thread can enter or exit a Not Runnable state</B></CAPTION>
</TR>
<TR>
<TH ALIGN="CENTER" VALIGN="TOP" WIDTH="50%">Enter a Not Runnable State</TH>
<TH ALIGN="CENTER" VALIGN="TOP" WIDTH="50%">Return to a Runnable State</TH>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="50%">A thread is blocking on I/O.</TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="50%">The I/O completes.</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="TOP">A thread uses the <B>wait()</B> method to wait on a condition variable.</TD>
<TD ALIGN="LEFT" VALIGN="TOP">The object which owns the variable calls <B>notify()</B> or <B>notifyAll()</B>.</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="TOP">You call a thread’s <B>sleep()</B> method.</TD>
<TD ALIGN="LEFT" VALIGN="TOP">The specified number of milliseconds elapses.</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="TOP">You call a thread’s <B>suspend()</B> method.</TD>
<TD ALIGN="LEFT" VALIGN="TOP">You call the <B>resume()</B> method.</TD>
</TR>
</TABLE>
<P>
</P>
<TABLE WIDTH="100%"><TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%"><IMG SRC="images/05-01i.jpg"></TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="95%">When a thread is in the Not Runnable state, it receives no processor time—even if the processor is available.</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Dead State</FONT></H4>
<P>There are two ways in which a thread can reach the Dead state.
</P>
<DL>
<DD><B>1.</B> Call the thread’s <B>stop()</B> method while the thread is in any of the three states (New, Runnable, Not Runnable).
<DD><B>2.</B> The Java VM executes the last line of code in the thread’s <B>run()</B> method. After the last line of code in the <B>run()</B> method has executed, the thread terminates.
</DL>
<P>The Java VM could get to the <B>run()</B> method’s last line of code by:</P>
<DL>
<DD><B>•</B> executing normally and finally reaching the end of the method; or
<DD><B>•</B> <I>not</I> catching an Exception or an Error object.
</DL>
<P><FONT SIZE="+1"><B>stop() and ThreadDeath</B></FONT></P>
<P>The following points pertain to the <B>stop()</B> method and <SMALL>ThreadDeath</SMALL>.</P>
<DL>
<DD><B>•</B> <B>stop()</B> kills a thread asynchronously.
<DD><B>•</B> <B>stop()</B> throws a <SMALL>ThreadDeath</SMALL> object (a thread will die if it receives the <SMALL>ThreadDeath</SMALL> exception).
<DD><B>•</B> The <SMALL>ThreadDeath</SMALL> object is actually a subclass of the Java <SMALL>Error</SMALL> object, not the <SMALL>Exceptionm</SMALL> object. Normally, applications do not catch <SMALL>Error</SMALL> objects.
<DD><B>•</B> Stopping a thread by throwing an object allows the stack to unwind nicely and allows synchronization objects to be destroyed cleanly.
</DL>
<TABLE WIDTH="100%">
<TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%"><IMG SRC="images/05-02i.jpg"></TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="95%">You should not use the <B>destroy()</B> method of the Thread class to terminate a thread.<BR><BR>The <B>destroy()</B> method does not perform any cleanup code, and any locks which are engaged will remain locked after <B>destroy()</B> has performed its work.</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">IsAlive[]</FONT></H4>
<P>The <B>isAlive()</B> method will return a <B>true</B> value if a thread is active. By “active,” it is meant that the thread is in a Runnable or Not Runnable state. If a thread is Dead or New, the <B>isAlive()</B> method returns a value of <B>false</B>.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch04/04-02.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/06-01.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 + -
显示快捷键?