13、守护线程

13、守护线程 daemon

1、线程分为守护线程,用户线程

2、虚拟机必须确保用户线程执行完成

3、虚拟机不用等守护线程执行完成

package com.testthread1;

import org.w3c.dom.ls.LSOutput;

import java.security.spec.RSAOtherPrimeInfo;

public class TestDaemon {
    public static void main(String[] args) {
        God god = new God();
        Person person= new Person();
        //创建守护线程
        Thread godthread = new Thread(god);
        //设置守护线程
        godthread.setDaemon(true);//默认false用户线程

        godthread.start();//守护线程启动

        new Thread(person).start();//用户线程

    }
}

class God implements  Runnable{
    @Override
    public void run() {
        while(true){
            System.out.println("守护线程");
        }
    }
}

class Person implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("普通线程run");
        }
        System.out.println("普通线程结束");
    }
}

  

posted @ 2022-07-17 16:21  颓废且努力奋斗的人  阅读(33)  评论(0)    收藏  举报