守护线程daemon

线程分为用户线程(正常都是用户线程)和守护线程。

虚拟机要等用户线程,不等守护线程。

setDaemon是boolean值,默认是false

public class TestThreadDaemon {
   public static void main(String[] args) {
       Story story = new Story();
       Thread thread1 = new Thread(story);
       thread1.setDaemon(true);
       thread1.start();
       new Thread(new Love()).start();
  }
}
class Love implements Runnable{
   @Override
   public void run() {
       for (int i = 0; i < 20; i++) {
           System.out.println("i love u");
      }
       System.out.println("愿你安好");
  }
}
class Story implements Runnable{
   @Override
   public void run() {
       while (true){
           System.out.println("its a romantic story");
      }

  }
}

 

posted on 2021-03-07 22:38  要给小八赚罐头钱  阅读(34)  评论(0)    收藏  举报