守護線程中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);
}
}