閱讀732 返回首頁    go 阿裏雲 go 技術社區[雲棲]


finally 不會執行的情況

 在 Java 中,finally 有沒有可能不會執行的,試看看下麵的情況

Java代碼
  1.     public class TestFinally {   
  2.     private static class DaemonThread extends Thread {   
  3.         @Override  
  4.         public void run() {   
  5.             try {   
  6.                 TimeUnit.MILLISECONDS.sleep(1000);   
  7.             } catch (InterruptedException e) {   
  8.                 e.printStackTrace();   
  9.             } finally {   
  10.                 System.out.println("In finally");   
  11.             }   
  12.         }   
  13.     }   
  14.        
  15.     public static void main(String args[]) {   
  16.         DaemonThread daemon = new DaemonThread();   
  17.         daemon.setDaemon(true);   
  18.         daemon.start();   
  19.     }   
  20. }

運行結果並沒有打印"in finally",因為在主線程結束時,後台線程同樣被結束掉,finally沒有機會執行。

最後更新:2017-04-02 06:51:53

  上一篇:go 利用Android 2.2新特性完成縮略圖
  下一篇:go Linux下C調用C++接口詳解