- 所有非守护线程结束之后,JVM退出
- thread.setDaemon(true);标志一个线程为守护线程
- 守护线程不能持有任何的资源
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
class Timer extends Thread {
@Override
public void run() {
while (true) {
System.out.println(LocalTime.now());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
}
}
public class ThreadDeamon {
public static void main(String[] args) throws InterruptedException {
System.out.println("main start");
Thread thread=new Timer();
thread.setDaemon(true);
thread.start();
Thread.sleep(5000);
System.out.println("main end");
}
}