1、线程分为守护线程和用户线程
2、虚拟机必须确保用户线程执行完毕
3、虚拟机不需要等待守护线程执行完毕
4、如后台记录操作日志、监控内存、垃圾回收gc等属于守护线程
package lesson04;
//守护线程
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
Thread thread = new Thread(god);
thread.setDaemon(true);//设置为守护线程
thread.start();
My my = new My();
new Thread(my).start();
}
}
//上帝:守护线程
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝在守护着你");
}
}
}
//自己:用户线程
class My implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("开心的活着");
}
System.out.println("goodbye ,world!");
}
}