笔记20200521001:多线程【守护线程】

package com.chengguo.线程;

/**
 * 线程守护【举例:上帝守护人类】
 */
public class Demo_20200522001_Daemon {
    public static void main(String[] args) {
        //创建线程
        God god = new God();
        Human human = new Human();

        Thread thread = new Thread(god);
        thread.setDaemon(true);//默认为false

        //上帝 守护线程启动
        thread.start();
        //human 线程启动
        new Thread(human).start();
    }


}

//上帝--->类
class God implements Runnable {
    @Override
    public void run() {
        while (true)
            System.out.println();
    }
}

//人类--->类
class Human implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("人类苟且活着……");
        }
        System.out.println("再见,这个美丽的世界……");
    }
}

  

 
posted @ 2020-06-15 00:35  忧桑の民工  阅读(109)  评论(0编辑  收藏  举报