守护线程Daemon的理解

1、守护线程伴随着主线程的销毁而销毁;

2、jvm虚拟机中有很多守护线程,随着main函数的结束而结束,自动回收栈中的内容。

Thread t1 = new Thread(){
           @Override
           public void run() {
               for (int i = 0; i < 10; i++) {
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   ad++;
                   System.out.println(ad);
               }
           }
       };
       t1.setDaemon(true);
       t1.start();
       Thread.sleep(2000);
       System.out.println(ad);

  

原文地址:https://www.cnblogs.com/shuchen007/p/9310580.html