守护线程是非守护线程的“助理”,只要jvm中存在非守护线程,则守护线程就存在,例如负责垃圾回收的线程,而当所有非守护线程执行完毕后,则守护线程也随之结束,不同生但共死
public class DaemonThread {
private static class UseThread extends Thread {
@Override
public void run() {
try {
while (!isInterrupted()) {
System.out.println(Thread.currentThread().getName()
+ " am extends Thread.");
}
System.out.println(Thread.currentThread().getName()
+ " interrupt flag is " + isInterrupted());
} finally {
System.out.println("...........finally");
}
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("...........................");
UseThread useThread = new UseThread();
useThread.setDaemon(true);
useThread.start();
Thread.sleep(5);
//useThread.interrupt();
}
}