java守护线程的学习

package cn.breeziness123.zhx;

/**
 * 守护线程:为用户线程服务的,JVN停止不用等待守护线程执行完毕
 * 默认:用户线程,JVN必须等待用户线程执行完毕才能停止
 */
public class ThreadDemo09 {

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

        Thread th1 = new Thread(god);
        Thread th2 = new Thread(you);

        th1.setDaemon(true);//默认为false,为true时设置为守护线程
        th1.start();
        th2.start();

    }


}

class God implements Runnable {


    @Override
    public void run() {

        for (int i = 1; i <= 365 * 1000000; i++) {

            System.out.println("god bless you" + "----->" + i);

        }

    }
}

class You implements Runnable {


    @Override
    public void run() {
        for (int i = 1; i < 365 * 100; i++) {
            System.out.println("你已经活了" + i + "天");
        }
        System.out.println("oooooooo.....");

    }
}

猜你喜欢

转载自blog.csdn.net/qq_40731414/article/details/86644663