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);
    }
}

 

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