package chapter2; public class Demo02 { public static void main(String[] args) { Thread th1=new Thread(){ @Override public void run() { //主线程 Thread th2=new Thread(){ //守护线程 @Override public void run() { try { Thread.sleep(1_000); System.out.println("T thread finish done."); } catch (InterruptedException e) { e.printStackTrace(); } } }; // 当th1.setDaemon(true),即t为Daemon线程时,主线程结束,守护线程也结束 th2.setDaemon(true); th2.start(); try { while (true) { System.out.println("Do some thing for health check."); Thread.sleep(1_000); } } catch (InterruptedException e) { e.printStackTrace(); } } }; th1.start(); } }