守护线程
-
- 含义
- 测试
含义
线程分为用户线程和守护线程
虚拟机必须确保用户线程执行完毕
虚拟机不用等待守护线程执行完毕
如,后台记录操作日志,监控内存,垃圾回收等待。。
测试
package com.Thread;
public class ThreadDaemon {
public static void main(String[] args) {
//创建用户线程
God god =new God();
You you=new You();
//使god变为守护线程
Thread thread =new Thread(god);
thread.setDaemon(true);//默认为false,都是用户线程
thread.start();
new Thread(you).start();
}
}
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("快乐的混日子");
}
System.out.println("go die");
}
}
class God implements Runnable{
@Override
public void run() {
while(true){
System.out.println("bless world");
}
}
}
注:b站狂神笔记。