当JVM中所有的线程都是守护线程的时候,JVM就可以退出了;
如果还有一个或以上的非守护线程则不会退出。
和普通线程写法的区别就是:
setDaemon(true);
demo
public class DeamonThread extends Thread {
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("线程执行了"+ i);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread thread = new DeamonThread();
thread.setDaemon(true);
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
280

被折叠的 条评论
为什么被折叠?



