守护线程(deamon)
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 如.后台记录操作日志,监控内存,垃圾回收等待…
package Thread;
public class TestDaemon {
public static void main(String[] args) {
YYou yYou = new YYou();
God god = new God();
Thread thread = new Thread(god);
thread.setDaemon(true);
thread.start();
new Thread(yYou).start();
}
}
class YYou implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("还活着");
}
System.out.println("End World");
}
}class God implements Runnable{
@Override
public void run() {
while (true) {
System.out.println("God守护着你");
}
}
}