?? 07-01.html
字號:
<tr> <td valign="top"> <input type="text" name="query" value="" size="11"> <input type="image" width=28 height=23 border=0 value="Go" name="Go" src="/images/go.gif" align=absmiddle><br><font face="arial, helvetica" size="2"> <SELECT NAME="metatags" style="font-size: 12; font-family: sans-serif;" size="1"> <option value="vstitle" SELECTED>Title <option value="vsauthor">Author <option value="vspublisher">Publisher <option value="vsisbn">ISBN </SELECT><br><br></font> </td> </tr></table></form><form action="" name="catlist"> <table width="145" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="top"> <img src="/images/jump_topica.gif" width="145" alt="" border="0"> </td> </tr> <tr> <td width="145"> <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="../ch06/06-01.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch08/08-01.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 7<BR>Thread Priority
</FONT></H2>
<P><BIG><B>Lesson Topics</B></BIG></P>
<DL>
<DD>• What is Thread Priority?
<DD>• How the Scheduler Works
<DD>• Time Slicing
<DD>• Starvation
</DL>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">What is Thread Priority</FONT></H3>
<P>The Java Scheduler is in charge of allocating a single CPU among the multiple threads of a Java application. In general, you need only be concerned with scheduling when the application has one or more CPU-intensive threads. In this case, it helps to know what is going on behind the scenes in the Java VM so that you can optimize the execution of your application and avoid problems.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Priority Values</FONT></H4>
<P>Every thread has a priority between <B>MIN_PRIORITY</B> and <B>MAX_PRIORITY</B>. As the priority value increases, so does the priority of the thread. Thus, a thread with a priority of 6 has greater priority than a thread with a priority of 4.</P>
<TABLE WIDTH="90%"><TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%"><IMG SRC="images/07-01i.jpg"></TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="85%">When a thread is created, it is born with the same priority as the thread that created it.</TD>
</TR>
</TABLE>
<P>The Java application can change the priority of a thread at runtime using the <B>setPriority()</B> method. The application can also query the thread for its priority level using the <B>getPriority()</B> method.</P>
<P><FONT SIZE="+1"><B>Maximum Priority</B></FONT></P>
<P>Threads within an Applet can have a maximum priority of 6. However, threads within a Java application can have the same maximum priority as the system, which is <B>MAX-PRIORITY (10)</B>.</P>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">How the Scheduler Works</FONT></H3>
<P>The Java Scheduler uses a <I>fixed</I> priority system. This is in contrast to a <I>variable</I> priority system, in which a Scheduler can dynamically adjust the priority of processes in order to avoid starvation.</P>
<P>Below is a list that provides a behind-the-scenes view of how the Scheduler chooses which thread to run:</P>
<DL>
<DD><B>•</B> The highest priority thread is chosen to run.
<DD><B>•</B> If a higher priority thread becomes Runnable, the scheduler will run it. This is called <I>preemptive scheduling</I>.
<DD><B>•</B> If a thread enters the 1) Not Runnable state, 2) stops, or 3) yields, the scheduler will choose the next highest priority thread to run.
<DD><B>•</B> On systems that support time slicing, a thread will become Not Runnable after its CPU time allotment has expired.
</DL>
<TABLE WIDTH="90%">
<TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%"><IMG SRC="images/07-02i.jpg"></TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="85%">Never rely on the fact that the underlying operating system performs time slicing in your application. This makes your Java program <I>nonportable</I>.</TD>
</TR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Time Slices & the Queue</FONT></H4>
<P>Internally, the Java VM has a separate queue for each priority level. When a thread (assume it is called <I>T</I>) finishes its time slice—or goes from the blocked to the unblocked state—it is placed at the back on the queue. As threads of the same priority finish their turn, Thread <I>T</I> moves toward the front of the queue. Eventually, Thread <I>T</I> will reach the front of the queue and get a chance to be picked by the Java Scheduler to run.</P>
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Time Slicing</FONT></H3>
<P>The implementation of threads largely depends on the threading mechanism of the underlying operating system on which the VM is running. Windows 95/98 and Windows NT support preemptive threading. Thus, Java threads running on Windows will be allocated a certain time slice and will be preempted when that period of time expires.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Sun Solaris = No Time Slicing</FONT></H4>
<P>Contrary to Windows, the Solaris version of Java (from Sun Microsystems) uses a simple threading package that does not perform any type of time slicing. Therefore, threads under the Solaris version of Java will not be interrupted preemptively. Threads under Solaris must be preempted by the Java application.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Solaris vs. Windows</FONT></H4>
<P>Consider a situation in which there is a two-threaded Java application, each thread having the same priority. If one thread runs continuously without ever explicitly yielding to the other thread, then—under Solaris—the second thread will never run. This second thread would be <I>starved</I> (described in the following section).</P>
<P>Unlike Solaris, however, under Windows the first and second thread would both get to run because the Windows-based VM will time slice between the two threads.</P>
<TABLE WIDTH="90%"><TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%"><IMG SRC="images/07-03i.jpg"></TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="85%">Differences between operating systems in the underlying Java threading mechanism introduces some system-dependent behavior to Java.</TD>
</TR>
</TABLE>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Starvation</FONT></H3>
<P>If a high priority thread runs continuously without relinquishing control of the CPU (by either sleeping or yielding), then equal and lower priority threads might never have a chance to execute. This is commonly known as <I>starvation</I>. You should write your applications to make attempts to allow all threads a chance to run.</P>
<TABLE WIDTH="90%"><TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%"><IMG SRC="images/07-04i.jpg"></TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="85%">If you have a CPU-intensive thread running in a Java Applet, you should use <B>setPriority()</B> to lower the priority of this thread, especially if this thread was created at the same priority level as the Applet itself.<BR><BR>The Applet should <I>never</I> be starved.</TD>
</TR>
</TABLE>
<P>
</P>
<TABLE WIDTH="90%"><TR>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%"><IMG SRC="images/07-05i.jpg"></TD>
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="85%">You should write your applications to make attempts to allow all threads a chance to run.</TD>
</TR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch06/06-01.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch08/08-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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -