概念:
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不必等待守护线程执行完毕
- 比如:后台操作日志,垃圾回收等…
示例:
public class Caraful {
public static void main(String[] args) {
God god=new God();
I my=new I();
Thread threadG=new Thread(god);
threadG.setDaemon(true);
threadG.start();
new Thread(my).start();
}
static class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("god is key");
}
}
}
static class I implements Runnable{
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println("第"+i+"天");
}
}
}
}