publicclassDeamonThread2{publicstaticvoidmain(String[] args)throws Exception{
Thread t =newThread(()->{
Thread innerThread =newThread(()->{try{while(true){
System.out.println("do some things for health ");
Thread.sleep(1_000);}}catch(InterruptedException e){
e.printStackTrace();}});// 如果不设置成守护线程 就一直死循环下去/**
* do some things for health
* mainfinished done
* do some things for health
* do some
* things for health
* do some things for health do some things for health
*/// innerThread.setDaemon(true);
innerThread.start();});// t.setDaemon(true);
t.start();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+"finished done");}}
线程分为用户线程和守护线程守护线程:用来服务于用户线程创建守护线程只需要将deamon设置为true,创建线程时deamon默认是falsepublic class DeamonThread { public static void main(String[] args) throws Exception{ Thread t = new Thread() { @Override public void run() { try {