⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap20.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<P>                 // intArray</P>
<P> 1 getfield #4 &lt;Field int intArray[]</FONT></P>
<P> 4 arraylength   // Pop array ref, push int array length</P>
<P> 5 iconst_2      // Push constant int 2</P>
<P> 6 idiv          // Pop two ints, divide, push int result</P>
<P>                 // Pop int into local var 1: </P>
<P> 7 istore_1      // int halfway = intArray.length/2;</P>
<P>&nbsp;</P>
<P>// This is the start of the code for the for loop</P>
<P> 8 iconst_0      // Push int constant 0</P>
<P> 9 istore_2      // Pop into local var 2: int i = 0;</P>
<P>10 goto 54       // Jump to for loop condition check</P>
<P>&nbsp;</P>
<P>// This is the start of the body of the for loop</P>
<P>13 aload_0       // Push the object ref at loc var 0 (the this ref)</P>
<P>                 // Pop object ref, push ref to instance variable</P>
<P>                 // intArray</P>
<P>14 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>17 arraylength   // Pop array ref, push int array length</P>
<P>18 iconst_1      // Push constant int 1</P>
<P>19 isub          // Pop two ints, subtract, push int result</P>
<P>20 iload_2       // Push int at local var 2 (i)</P>
<P>21 isub          // Pop two ints, subtract, push int result</P>
<P>                 // Pop int into local var 3:</P>
<P>22 istore_3      // int upperindex = intArray.length - 1 - i;</P>
<P>23 aload_0       // Push the object ref at loc var 0 (the this ref)</P>
<P>                 // Pop object ref, push ref to instance variable</P>
<P>                 // intArray</P>
<P>24 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>27 iload_3       // Push int at local var 3 (upperIndex)</P>
<P>28 iaload        // Pop index, arrayref, push int at arrayref[index]</P>
<P>                 // Pop into local var 4:</P>
<P>29 istore 4      // int save = intArray[upperIndex];</P>
<P>31 aload_0       // Push the object ref at loc var 0 (the this ref)</P>
<P>                 // Pop object ref, push ref to instance variable</P>
<P>                 // intArray</P>
<P>32 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>35 iload_3       // Push int at local var 3 (upperIndex)</P>
<P>36 aload_0       // Push the object ref at loc var 0 (the this ref)</P>
<P>                 // Pop object ref, push ref to instance variable</P>
<P>                 // intArray</P>
<P>37 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>40 iload_2       // Push int at local var 2 (i)</P>
<P>41 iaload        // Pop index, arrayref, push int at arrayref[index]</P>
<P>                 // Pop value, index, and arrayref,</P>
<P>                 // Set arrayref[index] = value:</P>
<P>42 iastore       // intArray[upperIndex] = intArray[i];</P>
<P>43 aload_0       // Push the object ref at loc var 0 (the this ref)</P>
<P>                 // Pop object ref, push ref to instance variable</P>
<P>                 // intArray</P>
<P>44 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>47 iload_2       // Push int at local var 2 (i)</P>
<P>48 iload 4       // Push int at local var 4 (save)</P>
<P>                 // Pop value, index, and arrayref,</P>
<P>                 // Set arrayref[index] = value:</P>
<P>50 iastore       // intArray[i] = save;</P>
<P>&nbsp;</P>
<P>// The body of the for loop is now done, this instruction does</P>
<P>// the incrementing of the loop variable i</P>
<P>51 iinc 2 1      // Increment by 1 int at local var 2: ++i;</P>
<P>&nbsp;</P>
<P>// This is the for loop condition check:</P>
<P>54 iload_2       // Push int at local var 2 (i)</P>
<P>55 iload_1       // Push int at local var 1 (halfway)</P>
<P>                 // Pop two ints, compare, jump if less than to</P>
<P>56 if_icmplt 13  // top of for loop body: for (; i &lt; halfway;)</P>
<P>&nbsp;</P>
<P>59 return        // return (void) from method</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>If you compare these bytecodes with the ones shown earlier for <FONT FACE="Courier New">KitchenSync</FONT>韘 <FONT FACE="Courier New">reverseOrder()</FONT> method, you will see that these bytecodes are in effect those of <FONT FACE="Courier New">KitchenSync</FONT> with the support for entering and exiting the monitor removed. Instructions at offset 0 through 56 of <FONT FACE="Courier New">HeatSync</FONT>韘 bytecodes correspond to instructions at offset 4 through 68 of <FONT FACE="Courier New">KitchenSync</FONT>韘 bytecodes. Because <FONT FACE="Courier New">HeatSync</FONT>韘 <FONT FACE="Courier New">reverseOrder()</FONT> method doesn韙 need a local variable slot to store the reference to the locked object, the local variable positions used by each method are different. The function of the instructions themselves, however, match up exactly.</P>
<P>Another difference between the two <FONT FACE="Courier New">reverseOrder()</FONT> methods is that the compiler doesn韙 create an exception table for <FONT FACE="Courier New">HeatSync</FONT>韘 <FONT FACE="Courier New">reverseOrder()</FONT> method. In <FONT FACE="Courier New">HeatSync</FONT>韘 case, an exception table isn韙 necessary. When this method is invoked, the Java Virtual Machine automatically acquires the lock on the <FONT FACE="Courier New">this</FONT> object. If this method completes abruptly, just as if it completes normally, the virtual machine will release the lock on the <FONT FACE="Courier New">this</FONT> object automatically.</P>
<P>A synchronized class (static) method operates in the same way as the synchronized instance method shown in the example above. The one difference is that instead of acquiring a lock on <FONT FACE="Courier New">this</FONT> (as there is no <FONT FACE="Courier New">this</FONT> in a class method), the thread must acquire a lock on the appropriate <FONT FACE="Courier New">Class</FONT> instance.</P>
<H3><EM><P>Coordination Support in Class Object</P>
</EM></H3><P>Class <FONT FACE="Courier New">Object</FONT> declares five methods that enable programmers to access the Java Virtual Machine韘 support for the coordination aspect of synchronization. These methods are declared public and final, so they are inherited by all classes. They can only be invoked from within a synchronized method or statement. In other words, the lock associated with an object must already be acquired before any of these methods are invoked. The methods are listed in Table 20-2.</P>
<P>Table 20-2. The wait and notify methods of class <FONT FACE="Courier New">Object</FONT></P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Method</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">void wait();</FONT></TD><TD VALIGN="TOP">Enter a monitor韘 wait set until notified by another thread</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">void wait(long timeout);</FONT> </TD><TD VALIGN="TOP">Enter a monitor韘 wait set until notified by another thread or <FONT FACE="Courier New">timeout</FONT> milliseconds elapses</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">void wait(long timeout, int nanos);</FONT> </TD><TD VALIGN="TOP">Enter a monitor韘 wait set until notified by another thread or <FONT FACE="Courier New">timeout</FONT> milliseconds plus <FONT FACE="Courier New">nanos</FONT> nanoseconds elapses</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">void notify();</FONT></TD><TD VALIGN="TOP">Wake up one thread waiting in the monitor韘 wait set. (If no threads are waiting, do nothing.)</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">void notifyAll();</FONT></TD><TD VALIGN="TOP">Wake up all threads waiting in the monitor韘 wait set. (If no threads are waiting, do nothing.)</TD></TR>
</TABLE>
<H3><EM><P>On the CD-ROM</P>
</EM></H3><P>The CD-ROM contains the source code examples from this chapter in a subdirectory of the <FONT FACE="Courier New">threads</FONT> directory.</P>
<H3><EM><P>The Resources Page</P>
</EM></H3><P>For more information about the material presented in this chapter, visit the resources<H3><EM> </EM></H3>page: <FONT FACE="Courier New"><A HREF="http://www.artima.com/insidejvm/threads.html" tppabs="http://www.artima.com/insidejvm/threads.html">http://www.artima.com/insidejvm/threads.html</A></FONT>.</P>
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap19.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap19.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A>&nbsp;<A HREF="appa.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/appa.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>

</TABLE>
<HR>
<P ALIGN=CENTER>&nbsp;<A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">COMPUTING
MCGRAW-HILL</A> | <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" TARGET="_top">Beta Books</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/computing/contact.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/contact.html" TARGET="_top">Contact Us</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" TARGET="_top">Order Information</A>
| <A HREF="http://mcgraw-hill.inforonics.com/compsearch.shtml" tppabs="http://mcgraw-hill.inforonics.com/compsearch.shtml" TARGET="_top">Online Catalog</A></P>

<P ALIGN=CENTER><FONT SIZE="-1"><A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">Computing McGraw-Hill</A> is an imprint of the <A HREF="http://www.pbg.mcgraw-hill.com/pbg-home.html" tppabs="http://www.pbg.mcgraw-hill.com/pbg-home.html" TARGET="_top">McGraw-Hill Professional Book Group</A>.</FONT></P>

<!-- begin footer -->
<HR>
<A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top"><IMG SRC="division-white.gif" tppabs="http://www.pbg.mcgraw-hill.com/images/division-white.gif" WIDTH="350" HEIGHT="44" ALT="A Division of the McGraw-Hill Companies" BORDER="0"></A><BR>
<FONT SIZE="-2">Copyright &copy; 1997 <A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top">
The McGraw-Hill Companies</A>. All rights reserved. Any use is subject to the 
<A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" TARGET="_top">
Terms of Use</A>; the corporation also has a comprehensive <A HREF="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" tppabs="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" TARGET="_top">
Privacy Policy</A> governing information we may collect from our customers.</FONT>
<!-- end footer -->
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -