1.1 守護線程示例,守護線程中的finally不一定會起作用

守護線程中finally不一定起作用,所以在守護線程的時候,不要在finally中釋放資源,可能不會執行
public class DaemonThread {
    public static class UseThread extends Thread{
        @Override
        public void run() {
            try {
                while(true){
                    System.out.println("thread");
                }
            } finally {
                //這裏不一定起作用
                System.out.println("守護線程結束");
            }
        }
    }
    static{
        UseThread useThread = new UseThread();
        useThread.setDaemon(true);
        useThread.start();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(2000);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章