0
点赞
收藏
分享

微信扫一扫

线程十二—— 线程守护

左小米z 2022-04-03 阅读 45
java

注意 : 上帝 守护你三万天  利用setDaemon方法将上帝线程设置为守护线程 当you线程死亡后 上帝线程一致守护着你。。。。

package com.yyf.ThreadSleep;


// 线程守护
public class TestDaemon {

    public static void main(String[] args) {
        God god = new God ();
        You you = new You ();
        Thread t1 = new Thread (god);
        // 设置上帝为守护线程
        t1.setDaemon (true);
        t1.start ();
        Thread t2 = new Thread (you);
        t2.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 ("你一直开心的活着");
        }
        System.out.println ("GoodBye,world");
    }
}

你一直开心的活着
你一直开心的活着
你一直开心的活着
你一直开心的活着
你一直开心的活着
你一直开心的活着
你一直开心的活着
GoodBye,world
上帝一直保佑着你
上帝一直保佑着你
上帝一直保佑着你
上帝一直保佑着你
上帝一直保佑着你
上帝一直保佑着你
上帝一直保佑着你

举报

相关推荐

0 条评论