例子如下:

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);
    }
}

 

相关文章:

  • 2021-07-10
  • 2021-12-26
  • 2021-10-14
  • 2022-02-13
  • 2021-12-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-07
  • 2021-10-10
  • 2021-09-15
  • 2022-01-11
  • 2021-07-05
相关资源
相似解决方案