一个例子让你懂java里面的守护线程

例子如下:

public class DemoThread {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("hello daemon thread");
                }
            }
        });
        thread.setDaemon(true);   //这一行去掉之后,JVM会一直运行,守护线程会随着主线程死亡而死亡
        thread.start();

        Thread.sleep(3000);
    }
}

 

posted on 2021-05-09 23:26  坚守梦想  阅读(57)  评论(0编辑  收藏  举报