Java 简明教程

Java - Daemon Thread

Daemon Thread in Java

为了支持用户线程,创建一个守护线程。它通常在后台工作,在所有其他线程关闭后终止。垃圾收集器就是守护线程的一个示例。

Characteristics of a Daemon Thread in Java

  1. 守护线程是低 priority thread

  2. 守护线程是一种服务提供程序线程,不应将其用作用户线程。

  3. 如果没有活动线程,JVM 会自动关闭守护线程,如果用户线程再次变得活跃,它会重新激活守护线程。

  4. 当所有用户线程完成时,守护线程无法阻止 JVM 退出。

Thread Class Methods for Java Daemon Thread

以下是 Thread class 在 Java 中为守护线程提供的使用方法 -

  1. Thread.setDaemon() Method :将此线程标记为守护线程或用户线程。

  2. Thread.isDaemon() Method :检查此线程是否为守护线程。

Example of Java Daemon Thread

在此示例中,我们创建了一个扩展 Thread 类的 ThreadDemo 类。在 main 方法中,我们创建了三个线程。由于我们未将任何线程设置为守护线程,因此没有线程被标记为守护线程。

package com.tutorialspoint;
class ThreadDemo extends Thread {
   ThreadDemo( ) {
   }
   public void run() {
      System.out.println("Thread  " + Thread.currentThread().getName()
         + ", is Daemon: " + Thread.currentThread().isDaemon());
   }
   public void start () {
      super.start();
   }
}
public class TestThread {
   public static void main(String args[]) {
	  ThreadDemo thread1 = new ThreadDemo();
	  ThreadDemo thread2 = new ThreadDemo();
	  ThreadDemo thread3 = new ThreadDemo();
	  thread1.start();
	  thread2.start();
	  thread3.start();
   }
}
Thread  Thread-1, is Daemon: false
Thread  Thread-0, is Daemon: false
Thread  Thread-2, is Daemon: false

More Example of Java Daemon Thread

Example 1

在此示例中,我们创建了一个扩展 Thread 类的 ThreadDemo 类。在 main 方法中,我们创建了三个线程。由于我们将一个线程设置为守护线程,因此一个线程将打印为守护线程。

package com.tutorialspoint;
class ThreadDemo extends Thread {
   ThreadDemo( ) {
   }

   public void run() {
      System.out.println("Thread  " + Thread.currentThread().getName()
         + ", is Daemon: " + Thread.currentThread().isDaemon());
   }
   public void start () {
      super.start();
   }
}
public class TestThread {
   public static void main(String args[]) {
	  ThreadDemo thread1 = new ThreadDemo();
	  ThreadDemo thread2 = new ThreadDemo();
	  ThreadDemo thread3 = new ThreadDemo();
      thread3.setDaemon(true);
	  thread1.start();
	  thread2.start();
	  thread3.start();
   }
}
Thread  Thread-1, is Daemon: false
Thread  Thread-2, is Daemon: true
Thread  Thread-0, is Daemon: false

Example 2

在此示例中,我们创建了一个扩展 Thread 类的 ThreadDemo 类。在 main 方法中,我们创建了三个线程。线程一旦启动,便无法将其设置为守护线程。由于我们在线程启动之后将其设置为守护线程,因此会引发运行时异常。

package com.tutorialspoint;
class ThreadDemo extends Thread {
   ThreadDemo( ) {
   }
   public void run() {
      System.out.println("Thread  " + Thread.currentThread().getName()
         + ", is Daemon: " + Thread.currentThread().isDaemon());
   }
   public void start () {
      super.start();
   }
}
public class TestThread {
   public static void main(String args[]) {
	   ThreadDemo thread1 = new ThreadDemo();
	   ThreadDemo thread2 = new ThreadDemo();
	   ThreadDemo thread3 = new ThreadDemo();
	   thread1.start();
	   thread2.start();
	   thread3.start();
      thread3.setDaemon(true);
   }
}
Exception in thread "main" Thread  Thread-1, is Daemon: false
Thread  Thread-2, is Daemon: false
Thread  Thread-0, is Daemon: false
java.lang.IllegalThreadStateException
	at java.lang.Thread.setDaemon(Unknown Source)
	at com.tutorialspoint.TestThread.main(TestThread.java:27)