其他分享
首页 > 其他分享> > 十九、守护线程

十九、守护线程

作者:互联网

 

public class ThreadDaemon {

    public static void main(String[] args) {

        You you = new You();
        God god = new God();

        Thread yt = new Thread(you);
        Thread gt = new Thread(god);

        //false:用户线程 true守护线程
        yt.setDaemon(false);
        gt.setDaemon(true);

        yt.start();
        gt.start();
    }
}

class God implements Runnable{

    @Override
    public void run() {
        while (true){
            System.out.println("God is watching over you");
        }
    }
}

class You implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("Live happily");
        }
        System.out.println("GoodBye World!");
    }
}

结果:

标签:gt,Thread,God,线程,new,十九,public,守护
来源: https://www.cnblogs.com/epiphany8/p/16272060.html