守护线程

image

thread.setDaemon(true); //默认是flase表示是用户线程,正常的线程都是用户线程

package com.guo.demo03.daemon;

import com.sun.xml.internal.stream.util.ThreadLocalBufferAllocator;

//测试守护线程
public class TestDaemon {

    public static void main(String[] args) {
        God god = new God();
        You you = new You();

        Thread thread = new Thread(god);
        thread.setDaemon(true);//默认是flase表示是用户线程,正常的线程都是用户线程

        thread.start();//上帝守护线程启动

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


    }
}



//上帝
class God implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("上帝保护着你");
        }
    }
}

//你
class You implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("你一生都开心的活着"+i);
        }
        System.out.println("-=======goodbye,world!===========");
    }
}
posted @ 2026-03-25 11:14  果子同志  阅读(10)  评论(0)    收藏  举报