Difference between User Threads and Daemon Threads in Java

Thread is a concept which comes under the topic Multithreading. There are two types of threads in java, one is User thread and other is Daemon thread. User threads are also called Non Daemon threads. I have already shared the most frequently asked multithreading interview questions in java.
In this article I will share difference between User thread and Daemon thread, important questions and examples. So let's find out

Read Also :  Multithreading Interview Questions in Java


Difference between User threads and Daemon threads in Java

1. Creation :  Any thread you create from main() method is a user thread. On the other hand, If you want to create a daemon thread then you need to call setDaemon(true) method as shown in the below example.

2. JVM exit : JVM will not exit until all user threads complete their execution. While JVM will exit without waiting for daemon thread to finish their work.

3.Thread Priority : User threads have high priority as compared to daemon threads. It means that daemon threads will not get CPU as easily as user thread can get.

4. Critical Task : Daemon threads are not used for any critical task. Any important task is assigned to user thread or non-daemon thread. Daemon threads are useful to perform background tasks. Do not perform any critical task on the daemon thread.

5. Termination : JVM will not force the user threads to terminate. User threads are closed by application or by itself(after finishing execution).On the other hand, daemon threads are terminated by the JVM if all the user threads have finished their task.

Examples of User Thread and Daemon Thread in Java

Below example demonstrates isDaemon() and setDaemon() methods usage.

public class DaemonThreadExample extends Thread {
    
    public static void main(String args[]) {
        System.out.println("Thread name is : "+ Thread.currentThread().getName());
        //Checking main thread is daemon or non-daemon thread(user thread)
        System.out.println("Is main thread daemon ? : "+ Thread.currentThread().isDaemon());
        
        // Creating two threads (by default they are user(non-daemon) threads)
        DaemonThreadExample t1 = new DaemonThreadExample();
        DaemonThreadExample t2 = new DaemonThreadExample();
        
        // Converting t1(user)thread to daemon thread
        t1.setDaemon(true);
        
        // Starting t1 and t2 threads 
        // Executing run method
        t1.start();
        t2.start();
    }
    
    public void run() { 
        // Checking threads are daemon or not 
        if (Thread.currentThread().isDaemon()) {
            System.out.println(Thread.currentThread().getName()+" is Daemon Thread");
        }
        else {
            System.out.println(Thread.currentThread().getName()+" is Non Daemon Thread");            
        }
        
    }
}


Output:
Thread name is : main
Is main thread daemon ? : false
Thread-0 is Daemon Thread
Thread-1 is Non Daemon Thread

Given below is another example showing you can not convert user thread to daemon thread or vice-versa after calling the start() method. If you try to do so then it will throw IllegalThreadStateException.


public class DaemonThreadExample extends Thread {
    
    public static void main(String args[]) {
        
        // Creating t1 thread (by default user(non-daemon) thread)
        DaemonThreadExample t1 = new DaemonThreadExample();

        // Starting t1 thread
        t1.start();

        // Converting t1 thread to daemon thread
        // after calling start() method
        // It will throw IllegalThreadStateException
        t1.setDaemon(true);
    }
    
    public void run() {
        if (Thread.currentThread().isDaemon()) {
            System.out.println(Thread.currentThread().getName()+" is Daemon Thread");
        }
    }
}
 

Output :
Exception in thread "main" java.lang.IllegalThreadStateException
    at java.base/java.lang.Thread.setDaemon(Thread.java:1458)
    at DaemonThreadExample.main(DaemonThreadExample.java:14)


Important Questions



Q Why main thread can not be converted into daemon thread.Although java allows other user threads to convert into daemon thread using setDaemon(boolean) method.
 
Because a thread can be set as daemon before it starts running. The main thread starts running as soon as the program starts. Hence it can not be set as daemon thread.

Q Why JVM terminates daemon thread when there is no user threads?

Daemon thread is used to provide services to the user threads while performing background supporting tasks. Critical tasks are performed by user threads. If there is no user threads why should JVM keep running daemon threads. That is why JVM terminates  daemon threads when there is no user threads.


Recap: Difference between User thread (Non-Daemon) and Daemon thread




User ThreadDaemon Thread
Creationcreated from main() methodcreated from setDaemon(true) method
JVM exitNo , until all user threads finish their executionYes, without waiting for daemon threads to finish their work
Thread PriorityHigh priority as compared to Daemon threadsLow priority threads
Critical TaskUser threads are used to perform critical task.Do not perform any critical task on the daemon thread.
TerminationJVM will not force user threads to terminate.JVM will force all daemon threads to terminate if their is no user threads.

Please mention in the comments if you have any questions regarding difference between User threads (Non-daemon) and Daemon threads in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry