===============
== ffff65535 ==
===============
十六个一[1111111111111111]

守护(daemon)线程

thread
  • 线程分为用户线程和守护线程
  • 虚拟机必须确保用户线程执行完毕
  • 虚拟机不用等待守护线程执行完毕
    • 如:后台记录操作日志,监控内存,垃圾回收等待…
public class TestThreadDaemon {
    public static void main(String[] args) {
        God god = new God();
        Thread godThread = new Thread(god);
        // 默认是false,表示用户线程
        godThread.setDaemon(true);
        godThread.start();

        // 你 用户线程启动
        new Thread(new You()).start();

        // 用户线程执行完毕,守护线程也会停止
    }
}

// 上帝 守护线程
class God implements Runnable {

    @Override
    public void run() {
        while (true) {
            System.out.println("上帝保佑着你");
        }
    }
}

// 你 用户线程
class You implements Runnable {
    @Override
    public void run() {
        System.out.println("=======hello world=======");
        for (int i = 0; i < 35600; i++) {
            System.out.println("你一生都开心活着");
        }
        System.out.println("=======goodbye world=======");
    }
}

参考资料:

https://www.bilibili.com/video/BV1V4411p7EF?p=17