Difference between Daemon Thread vs User Thread in Java?

A thread is used to perform parallel execution  in Jaa e.g. while rendering screen your program is also downloading the data from the internet in the background. There are two types of threads in Java, user thread and daemon thread, both of which can use to implement parallel processing in Java depending upon priority and importance of the task. The main difference between a user thread and a daemon thread is that your Java program will not finish execution until one of the user thread is live. JVM will wait for all active user threads to finish their execution before it shutdown itself. On the other hand, a daemon thread doesn't get that preference, JVM will exit and close the Java program even if there is a daemon thread running in the background. They are treated as low priority threads in Java, that's why they are more suitable for non-critical background jobs. In this article, we will learn some key difference between user and daemon thread from Java multithreading and interview perspective.

User thread vs Daemon threads in Java

As I told, the user threads are the normal threads you see and use in your day to day coding. Any thread you create from main thread becomes a user thread. If you want to create a daemon thread then you need to call the setDaemon(true) method to make it a daemon.

Another key point to remember is that any new thread spawned from a daemon thread also becomes a daemon because thread inherits their daemon property from the thread which creates them. Since main thread is a non-daemon thread, any thread spawned from main also becomes non-daemon or a user thread.

Let's see a couple of more difference between user and daemon thread in Java.

1) JVM doesn't wait for daemon thread to finish 
First and foremost difference is that JVM will not wait for daemon thread to finish their work but it will wait for any active user thread. You might have noticed this behavior while running Java program in Eclipse that even if your main thread has finished the top right button is still red, showing that Java program is still running. This is due to any user thread spawned from the main thread, but with main thead you don't see that red dot in Eclipse.

Here is a code snippet from Eclipse which demonstrate that:

Daemon thread example in Java

2) JVM itself creates Daemon thrad 
Another difference between them is that daemon thread is mostly created by JVM e.g. for some garbage collection job. On the other hand user thread is usually created by the application for executing some task concurrently. You can also check The Definitive guide to Java Performance to learn more about what JVM does during shutdown.

3) Daemon thread is not used for critical task 
Another key difference between daemon and user thread is that daemons are not used for any critical task. Any important task is done by non-daemon or user thread. A daemon thread is generally used for some background tasks which are not critical.

4) Thread Priority 
As compared to user thread, the daemon threads are low priority thread.  Which means they won't get CPU as easily as a user thread can get. See Java Thread 3rd edition to learn more about Thread priority and how CPU is allocated betwee threads.

Difference between Daemon Thread vs User Thread in Java?

5) JVM closes daemon thread
The user thread is closed by application or by itself but JVM will force daemon thread to terminate if all user threads have finished their execution. A daemon thread cannot keep the JVM running but a user thread can. This is the most critical difference between daemon and user thread which you should remember.

Java Program to create Daemon Thread

Here is our sample Java program to demonstrate the real difference between a daemon and user thread in Java. It also shows you how to create a daemon thread in Java or how to make a normal thread daemon by calling the setDaemon(true) in Java. By default any thread created from the main thread is non-daemon or user thread.

 * Java Program to demonstrate difference beween a daemon and a user thread .
 * This program also tells how to make a thread daemon in Java. 
 * @author WINDOWS 8
public class DaemonThreadDemo{

    public static void main(String[] args) throws InterruptedException {

        // main thread is a non-daemon thread
        String name = Thread.currentThread().getName();
        boolean isDaemon = Thread.currentThread().isDaemon();

        System.out.println("name: " + name + ", isDaemon: " + isDaemon);

        // Any new thread spawned from main is also non-daemon or user thread
        // as seen below:
        Runnable task = new Task();
        Thread t1 = new Thread(task, "T1");
        System.out.println("Thread spawned from main thread");
        System.out.println("name: " + t1.getName() + ", isDaemon: " + t1.isDaemon());

        // though you can make a daemon thread by calling setDaemon()
        // before starting it as shown below:

        // let's wait for T1 to finish

    private static class Task implements Runnable {

        public void run() {
            Thread t = Thread.currentThread();
            System.out.println("Thread made daemon by calling setDaemon() method");
            System.out.println("name: " + t.getName() + ", isDaemon: " + t.isDaemon());

            // Any new thread created from daemon thread is also daemon
            Thread t2 = new Thread("T2");
            System.out.println("Thread spawned from a daemon thread");
            System.out.println("name: " + t2.getName() + ", isDaemon: " + t2.isDaemon());




name: main, isDaemon: false
Thread spawned from main thread
name: T1, isDaemon: false
Thread made daemon by calling setDaemon() method
name: T1, isDaemon: true
Thread spawned from a daemon thread
name: T2, isDaemon: true

That's all about the difference between daemon and a non-daemon thread in Java. One of the important thing to remember that JVM will forcefully terminate all the active daemon thread if there is no user thread is pending execution or active, so don't perform any critical task on daemon thread.

Also, remember, that thread inherit daemon property from the thread which creates it. For example, if you spawn a new thread from main() then they will be non-daemon or user thread and you need to call the setDaemon(true) to make them daemon, on the other hand, if you spawn a new thread from a daemon thread then by default it will be a daemon thread.

Other Java Thread tutorials for Programmers
  • How to join threads in Java? (solution)
  • How to pause a Thread in Java? (answer)
  • How to stop a Thread in Java? (answer)
  • How to do inter thread communication in Java? (answer)
  • Difference between extends Thread and implements Runnable? (answer)
  • How to use multiple threads in Java? (example)
  • Difference between Thread.start() and run() in Java? (answer)

No comments:

Post a Comment