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

📄 chap20.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<P> 4 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> 5 getfield #4 &lt;Field int intArray[]</FONT></P>
<P> 8 arraylength    // Pop array ref, push int array length </P>
<P> 9 iconst_2       // Push constant int 2</P>
<P>10 idiv          // Pop two ints, divide, push int result </P>
<P>                 // Pop int into local var 3:</P>
<P>11 istore_3      // int halfway = intArray.length/2;</P>
<P>&nbsp;</P>
<P>// This is the start of the code for the for loop</P>
<P>12 iconst_0      // Push constant int 0</P>
<P>13 istore 4      // Pop into local var 2: int i = 0;</P>
<P>15 goto 65       // Jump to for loop condition check</P>
<P>&nbsp;</P>
<P>// This is the start of the body of the for loop</P>
<P>18 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>19 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>22 arraylength   // Pop array ref, push int array length </P>
<P>23 iconst_1      // Push constant int 1</P>
<P>24 isub          // Pop two ints, subtract, push int result</P>
<P>25 iload 4       // Push int at local var 4 (i)</P>
<P>27 isub          // Pop two ints, subtract, push int result</P>
<P>                 // Pop int into local var 5:</P>
<P>28 istore 5      // int upperindex = intArray.length - 1 - i;</P>
<P>30 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>31 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>34 iload 5       // Push int at local var 5 (upperIndex)</P>
<P>36 iaload        // Pop index, arrayref, push int at arrayref[index]</P>
<P>                 // Pop into local var 6:</P>
<P>37 istore 6      // int save = intArray[upperIndex];</P>
<P>39 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>40 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>43 iload 5       // Push int at local var 5 (upperIndex)</P>
<P>45 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>46 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>49 iload 4       // Push int at local var 4 (i)</P>
<P>51 iaload        // Pop index, arrayref, push int at arrayref[index]</P>
<P>                 // Set arrayref[index] = value:</P>
<P>52 iastore       // intArray[upperIndex] = intArray[i];</P>
<P>53 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>54 getfield #4 &lt;Field int intArray[]</FONT></P>
<P>57 iload 4       // Push int at local var 4 (i)</P>
<P>59 iload 6       // Push int at local var 6 (save)</P>
<P>                 // Set arrayref[index] = value:</P>
<P>61 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>62 iinc 4 1      // Increment by 1 int at local var 4: ++i;</P>
<P>&nbsp;</P>
<P>// This is the for loop condition check:</P>
<P>65 iload 4       // Push int at local var 4 (i)</P>
<P>67 iload_3       // Push int at local var 3 (halfway)</P>
<P>                 // Pop two ints, compare, jump if less than to</P>
<P>68 if_icmplt 18  // top of for loop body: for (; i &lt; halfway;)</P>
<P>// The code of the synchronized block ends here</P>
<P>&nbsp;</P>
<P>// The next two instructions unlock the object, making it available</P>
<P>// for other threads. The reference to the locked object was stored</P>
<P>// in local variable 1 above.</P>
<P>71 aload_1            // Push local var 1 (the this reference)</P>
<P>72 monitorexit        // Pop ref, unlock object</P>
<P>73 return             // return normally from method</P>
<P>&nbsp;</P>
<P>// This is a catch clause for any exception thrown (and not caught</P>
<P>// from within the synchronized block. If an exception is thrown,</P>
<P>// the locked object is unlocked, making it available for other</P>
<P>// threads.</P>
<P>74 aload_1            // Push local var 1 (the this reference)</P>
<P>75 monitorexit        // Pop ref, unlock object</P>
<P>76 athrow             // rethrow the same exception</P>
<P>&nbsp;</P>
<P>// The exception table shows the "catch all" clause covers the</P>
<P>// entire synchronized block, from just after the lock is acquired</P>
<P>// to just before the lock is released.</P>
<P>Exception table:</P>
<P>from to target type</P>
<P>  4  71   74   any</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>Note that a catch clause ensures the locked object will be unlocked even if an exception is thrown from within the synchronized block. No matter how the synchronized block is exited, the object lock acquired when the thread entered the block will definitely be released.</P>
<H3><P>Synchronized Methods</P>
</H3><P>To synchronize an entire method, you just include the synchronized keyword as one of<H3> </H3>the method qualifiers, as in:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New">// On CD-ROM in file threads/ex1/HeatSync.java
<P>class HeatSync {</P>
<P>&nbsp;</P>
<P>    private int[] intArray = new int[10];</P>
<P>&nbsp;</P>
<P>    synchronized void reverseOrder() {</P>
<P>        int halfWay = intArray.length / 2;</P>
<P>        for (int i = 0; i &lt; halfWay; ++i) {</P>
<P>            int upperIndex = intArray.length - 1 - i;</P>
<P>            int save = intArray[upperIndex];</P>
<P>            intArray[upperIndex] = intArray[i];</P>
<P>            intArray[i] = save;</P>
<P>        }</P>
<P>    }</P>
<P>    // ...</P>
<P>}</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The Java Virtual Machine does not use any special opcodes to invoke or return from synchronized methods. When the virtual machine resolves the symbolic reference to a method, it determines whether the method is synchronized. If so, the virtual machine acquires a lock before invoking the method. For an instance method, the virtual machine acquires the lock associated with the object upon which the method is being invoked. For a class method, it acquires the lock associated with the class to which the method belongs (it locks a <FONT FACE="Courier New">Class</FONT> object). After a synchronized method completes, whether it completes by returning or by throwing an exception, the virtual machine releases the lock.</P>
<P>Here are the bytecodes that <FONT FACE="Courier New">javac</FONT> generates for <FONT FACE="Courier New">HeatSync</FONT>韘 <FONT FACE="Courier New">reverseOrder()</FONT> method:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New"> 0 aload_0       // Push the object ref at loc var 0 (the this ref)
<P>                 // Pop object ref, push ref to instance variable</P>

⌨️ 快捷键说明

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