其他分享
首页 > 其他分享> > 守护线程_daemon

守护线程_daemon

作者:互联网

守护线程_daemon

测试案例:

package multithreading;

// 测试守护线程
// 上帝守护你
public class TestDaemon {

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

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

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

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

// 上帝
class God implements Runnable{

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

// 你
class You2 implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("你的一生");
        }
        System.out.println("========gooodbye! world!=========");
    }
}

运行结果:

标签:daemon,God,System,线程,new,public,守护
来源: https://www.cnblogs.com/CH0701/p/15038395.html