守护进程:当进程中不存在非守护线程了,则守护线程自动销毁;

public class DaemonThread extends Thread{

    private int i =0;
public void run(){
try {
while(true){
i++;
System.out.println("i="+i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("test");
e.printStackTrace();
}
} public static void main(String[] args) throws InterruptedException {
DaemonThread thread = new DaemonThread();
thread.setDaemon(true);
thread.start();
Thread.sleep(2000);
System.out.println("我离开thread对象也不再打印了,也就是停止了!");
}
}
05-19 20:46