守护线程中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);
}
}