LetsCoding.cn

          天地之間有桿秤,拿秤砣砸老百姓。

          JVM中finally子句介紹

          歡迎來(lái)到“Under The Hood”第七期。本期我們介紹JVM處理finally子句的方式及相關(guān)字節(jié)碼。你可能需要閱讀往期的文章才能更好的理解本文。

          finally子句

          JVM執(zhí)行Java字節(jié)碼時(shí),它有幾種方式可以退出一個(gè)代碼塊(花括號(hào)中間的語(yǔ)句)。其中之一,就是簡(jiǎn)單的執(zhí)行完其中所有的語(yǔ)句,然后退出代碼塊。第二種,JVM可能會(huì)在代碼塊中間的任何一處,遇到像break,continue,return之類的語(yǔ)句,強(qiáng)制它跳出該代碼塊。第三種,JVM可能會(huì)在執(zhí)行過(guò)程中,出現(xiàn)了異常,然后它跳轉(zhuǎn)到匹配的catch子句,或者沒(méi)有找到相應(yīng)的catch子句,直接退出當(dāng)前線程。由于單個(gè)代碼塊有如此多的潛在退出點(diǎn)(exit point),擁有一個(gè)簡(jiǎn)單的方式來(lái)表達(dá)“無(wú)論代碼塊以什么方式退出,有些事情總能發(fā)生”是很值得的。然后就有了try-finally子句。

          try-finally子句的用法:

          • 把擁有多個(gè)退出點(diǎn)的代碼塊放在try塊中,并且
          • 把無(wú)論try塊怎么退出,始終能被執(zhí)行的代碼放在finally塊中。

          例如:

          1. try {
          2.     // Block of code with multiple exit points
          3. }
          4. finally {
          5.     // Block of code that is always executed when the try block is exited,
          6.     // no matter how the try block is exited
          7. }

          如果try塊有多個(gè)catch子句與之關(guān)聯(lián),你就必須把finally子句放在所有catch子句的后面:

          1. try {
          2.     // Block of code with multiple exit points
          3. }
          4. catch (Cold e) {
          5.     System.out.println("Caught cold!");
          6. }
          7. catch (APopFly e) {
          8.     System.out.println("Caught a pop fly!");
          9. }
          10. catch (SomeonesEye e) {
          11.     System.out.println("Caught someone's eye!");
          12. }
          13. finally {
          14.     // Block of code that is always executed when the try block is exited,
          15.     // no matter how the try block is exited.
          16.     System.out.println("Is that something to cheer about?");
          17. }

          在try塊的代碼執(zhí)行過(guò)程中,先由catch子句負(fù)責(zé)處理拋出的異常,然后再執(zhí)行finall子句中的代碼。例如,如果上述代碼中的try塊拋出Cold異常,控制臺(tái)將會(huì)輸出如下信息:

          1. Caught cold!
          2. Is that something to cheer about?

          字節(jié)碼中的try-finally子句

          在字節(jié)碼中,finally子句扮演著方法中子程序的角色。在try塊和它所關(guān)聯(lián)的catch子句中的每個(gè)退出點(diǎn),代表finally子句的子程序會(huì)被調(diào)用。一旦最后一條語(yǔ)句執(zhí)行完成,且沒(méi)有拋出異常,沒(méi)有執(zhí)行return、continue、和break,則finally子句調(diào)用結(jié)束,子程序返回。JVM從調(diào)用子程序的指令后下一條指令繼續(xù)執(zhí)行,這樣try塊就能以適當(dāng)?shù)姆绞酵顺隽恕?/p>

          讓JVM跳轉(zhuǎn)到子程序的操作碼是jsr指令。jsr指令有2個(gè)單字節(jié)操作數(shù),它們組成子程序入口地址到j(luò)sr跳轉(zhuǎn)指令的偏移量。jsr_w指令是jsr的變種,功能和jsr相同,但有4個(gè)單字節(jié)操作數(shù)。當(dāng)JVM遇到j(luò)sr或jsr_w指令,它把返回地址(return address,即jsr或jsr_w指令的下一條指令地址)壓入棧中,然后從子程序的入口處繼續(xù)往下執(zhí)行。

          JVM在子程序完成之后,調(diào)用ret指令,從子程序返回。ret指令擁有一個(gè)操作數(shù),它是一個(gè)索引,指向存儲(chǔ)返回地址的本地變量。處理finally子句的操作碼總結(jié)如下:

          OPCODE
          OPERAND(S)
          DESCRIPTION
          jsr branchbyte1, branchbyte2 pushes the return address, branches to offset
          jsr_w branchbyte1, branchbyte2, branchbyte3, branchbyte4 pushes the return address, branches to wide offset
          ret index returns to the address stored in local variable index

          不要把子程序和Java里的方法搞混了,Java中的方法會(huì)使用不同于子程序的指令集。invokevirtual或invokeonvirtual指令用來(lái)處理方法調(diào)用,而return,areturn或ireturn指令用來(lái)處理方法返回。jsr指令不會(huì)導(dǎo)致方法被調(diào)用,而會(huì)使JVM跳轉(zhuǎn)到同一方法中不同的指令處。類似的,ret指令不會(huì)從方法中返回,它讓JVM從子程序返回到j(luò)sr指令的下一條指令處。實(shí)現(xiàn)finally子句的字節(jié)碼之所以被稱為子程序,是因?yàn)樗鼈兛雌饋?lái)像是單個(gè)方法的字節(jié)碼流中的很小的子程序。

          你可能會(huì)認(rèn)為ret指令應(yīng)該把返回地址從棧中彈出,因?yàn)槟抢锸撬籮sr指令壓入的地方。但是,你錯(cuò)了。

          每個(gè)子程序的開(kāi)始,返回地址就被從棧頂彈出,并保存到本地變量中。ret指令會(huì)從同一個(gè)本地變量獲得子程序的返回地址。這種不對(duì)稱的返回地址使用方式是必須的,因?yàn)閒inall子句(子程序)本身可以拋出異常,或者包含return,break或continue語(yǔ)句。由于這種可能性,被jsr指令壓入棧中的返回地址必須立即從棧頂移除,這樣當(dāng)JVM由于break,continue,return語(yǔ)句或拋出的異常而從finally子句中退出時(shí),返回地址就不會(huì)依舊保存在棧中。因此,返回地址在子程序執(zhí)行的開(kāi)始,就被保存到本地變量中。

          作為示例,參考下面的代碼,它包含一個(gè)擁有break語(yǔ)句的finally子句。無(wú)論傳給surpriseTheProgrammer()方法的參數(shù)是什么,這段代碼的結(jié)果總是false。

          1. static boolean surpriseTheProgrammer(boolean bVal) {
          2.     while (bVal) {
          3.         try {
          4.             return true;
          5.         }
          6.         finally {
          7.             break;
          8.         }
          9.     }
          10.     return false;
          11. }

          上面的例子顯示了,為什么要在子程序的開(kāi)始處,就把返回地址保存到本地變量中。因?yàn)閒inally子句從break返回,它不會(huì)執(zhí)行ret指令。結(jié)果就是JVM不會(huì)執(zhí)行“return true”語(yǔ)句,它會(huì)執(zhí)行break語(yǔ)句,并繼續(xù)往下執(zhí)行,結(jié)束while循環(huán),執(zhí)行“return false”。

          以break語(yǔ)句退出finally子句的方式,跟以return,continue或者拋出異常的方式退出是一樣的。如果finally子句以這四種方式之一退出,子句中的ret指令永遠(yuǎn)都不會(huì)被執(zhí)行到。鑒于此,ret指令不能保證肯定會(huì)被執(zhí)行,JVM不能指望它去移除棧中的返回地址。因此,返回地址在子程序執(zhí)行的開(kāi)始,就被保存到本地變量中。

          作為一個(gè)完整的例子,請(qǐng)看如下方法,它包含一個(gè)有2個(gè)退出點(diǎn)的try塊。

          1. static int giveMeThatOldFashionedBoolean(boolean bVal) {
          2.     try {
          3.         if (bVal) {
          4.             return 1;
          5.         }
          6.         return 0;
          7.     }
          8.     finally {
          9.         System.out.println("Got old fashioned.");
          10.     }
          11. }

          它的字節(jié)碼如下:

          1. // The bytecode sequence for the try block:
          2. 0 iload_0               // Push local variable 0 (arg passed as divisor)
          3. 1 ifeq 11               // Push local variable 1 (arg passed as dividend)
          4. 4 iconst_1              // Push int 1
          5. 5 istore_3              // Pop an int (the 1), store into local variable 3
          6. 6 jsr 24                // Jump to the mini-subroutine for the finally clause
          7. 9 iload_3               // Push local variable 3 (the 1)
          8. 10 ireturn               // Return int on top of the stack (the 1)
          9. 11 iconst_0              // Push int 0
          10. 12 istore_3              // Pop an int (the 0), store into local variable 3
          11. 13 jsr 24                // Jump to the mini-subroutine for the finally clause
          12. 16 iload_3               // Push local variable 3 (the 0)
          13. 17 ireturn               // Return int on top of the stack (the 0)
          14. // The bytecode sequence for a catch clause that catches any kind of exception
          15. // thrown from within the try block.
          16. 18 astore_1              // Pop the reference to the thrown exception, store
          17.                          // into local variable 1
          18. 19 jsr 24                // Jump to the mini-subroutine for the finally clause
          19. 22 aload_1               // Push the reference (to the thrown exception) from
          20.                            // local variable 1
          21. 23 athrow                // Rethrow the same exception
          22. // The miniature subroutine that implements the finally block.
          23. 24 astore_2              // Pop the return address, store it in local variable 2
          24. 25 getstatic #8          // Get a reference to java.lang.System.out
          25. 28 ldc #1                // Push < string "Got old fashioned." > from the constant pool
          26. 30 invokevirtual #7      // Invoke System.out.println()
          27. 33 ret 2                 // Return to return address stored in local variable 2

          try塊的字節(jié)碼中含有2個(gè)jsr指令,另外一個(gè)jsr指令在catch子句中。catch子句是由編譯器自動(dòng)添加的,因?yàn)槿绻趖ry塊執(zhí)行過(guò)程中拋出異常,finally塊必須任然被執(zhí)行。因此catch子句僅僅調(diào)用代表finally塊的子程序,然后拋出相同的異常。下面所示的giveMeThatOldFashionedBoolean() 方法的異常表說(shuō)明,0到17行所拋出的任何異常,都由從18行開(kāi)始的catch子句來(lái)處理。

          FROM
          TO
          TARGET
          TYPE
          0 18 18 any

          可以看出,finally子句的字節(jié)碼,以彈出并保存棧頂返回地址到本地變量開(kāi)始,以從本地變量2中取得返回地址的ret指令返回。

          本文譯自:Try-finally clauses defined and demonstrated

          譯者注:基于字節(jié)碼校驗(yàn)方面的考量,JVM中的jsr/ret指令在Java 6.0時(shí),已經(jīng)被移除。

          本文出自:碼農(nóng)合作社 》JVM中finally子句介紹,轉(zhuǎn)載請(qǐng)注明。

          posted on 2014-06-08 02:42 Rolandz 閱讀(2187) 評(píng)論(1)  編輯  收藏 所屬分類: 編程實(shí)踐

          評(píng)論

          # re: JVM中finally子句介紹 2014-06-14 12:23 泡菜

          好好學(xué)習(xí),一定能成功的!相信自己,相信一定有發(fā)展的。  回復(fù)  更多評(píng)論   

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿(1)

          隨筆分類(12)

          隨筆檔案(19)

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 保靖县| 长兴县| 尼玛县| 辛集市| 景宁| 武义县| 望城县| 曲麻莱县| 吴川市| 大名县| 卓资县| 文成县| 桐柏县| 抚宁县| 多伦县| 务川| 乌兰浩特市| 临海市| 承德县| 丰宁| 瓦房店市| 太原市| 南部县| 安达市| 澄江县| 阳春市| 莱芜市| 澎湖县| 宾阳县| 南皮县| 吉水县| 沙坪坝区| 靖安县| 马龙县| 克拉玛依市| 西乌珠穆沁旗| 加查县| 东乡族自治县| 调兵山市| 禄丰县| 茶陵县|