Daemon Threads in Java: Background Services for User Threads

Learn about daemon threads in Java, which act as service provider threads running in the background to support user threads. Their lifecycle is closely tied to user threads; once all user threads complete execution, the JVM automatically terminates daemon threads. Discover common examples of daemon threads, such as garbage collection (gc) and finalizer threads, and understand their role in optimizing resource management in Java applications.



Daemon Thread in Java

A daemon thread in Java is a service provider thread that runs in the background and provides services to user threads. Its lifecycle depends on the existence of user threads; when all user threads finish execution, the JVM terminates the daemon thread automatically.

There are several daemon threads in Java that run automatically, such as garbage collection (gc) and finalizer threads.

You can monitor these threads and more using tools like jconsole, which provides insights into loaded classes, memory usage, and running threads.

Points to Remember for Daemon Thread in Java

  • It provides background services to user threads.
  • Its lifecycle is dependent on user threads.
  • It is a low priority thread.
  • If there are no user threads left, the JVM terminates the daemon thread.

Methods for Java Daemon Thread by Thread class

No. Method Description
1. public void setDaemon(boolean status) Marks the current thread as a daemon thread or user thread.
2. public boolean isDaemon() Checks if the current thread is a daemon thread.

Example: Simple Daemon Thread in Java

TestDaemonThread1.java

public class TestDaemonThread1 extends Thread {
    public void run() {
        if (Thread.currentThread().isDaemon()) {
            System.out.println("Daemon thread work");
        } else {
            System.out.println("User thread work");
        }
    }

    public static void main(String[] args) {
        TestDaemonThread1 t1 = new TestDaemonThread1();
        TestDaemonThread1 t2 = new TestDaemonThread1();
        TestDaemonThread1 t3 = new TestDaemonThread1();

        t1.setDaemon(true); // t1 is now a daemon thread

        t1.start();
        t2.start();
        t3.start();
    }
}
        
Output

Daemon thread work
User thread work
User thread work
        

In this example, t1 is set as a daemon thread using setDaemon(true). When started, it prints "Daemon thread work".

Example: IllegalThreadStateException

TestDaemonThread2.java

public class TestDaemonThread2 extends Thread {
    public void run() {
        System.out.println("Name: " + Thread.currentThread().getName());
        System.out.println("Daemon: " + Thread.currentThread().isDaemon());
    }

    public static void main(String[] args) {
        TestDaemonThread2 t1 = new TestDaemonThread2();
        TestDaemonThread2 t2 = new TestDaemonThread2();

        t1.start();
        t1.setDaemon(true); // Throws IllegalThreadStateException

        t2.start();
    }
}
        
Output

Exception in thread "main" java.lang.IllegalThreadStateException
    at java.base/java.lang.Thread.setDaemon(Thread.java:1367)
    at TestDaemonThread2.main(TestDaemonThread2.java:13)
        

In this example, trying to set t1 as a daemon thread after starting it results in IllegalThreadStateException.