A thread is used to perform parallel execution in Java 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 the 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 threads 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 differences between user and daemon thread from Java multithreading and interview perspective.
Another key point to remember is that any new thread spawned from a daemon thread also becomes a daemon because the thread inherits their daemon property from the thread which creates them. Since the main thread is a non-daemon thread, any thread spawned from the main also becomes a non-daemon or a user thread.
Let's see a couple of more differences between the user and daemon thread in Java.
Here is a code snippet from Eclipse which demonstrates that:
That's all about the difference between a daemon and a non-daemon thread in Java. One of the important things to remember that JVM will forcefully terminate all the active daemon threads if there is no user thread that is pending execution or active, so don't perform any critical task on the daemon thread.
Also, remember, that thread inherits 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.
User thread vs Daemon threads in Java
As I told you, the user threads are the normal threads you see and use in your day-to-day coding. Any thread you create from the 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 the thread inherits their daemon property from the thread which creates them. Since the main thread is a non-daemon thread, any thread spawned from the main also becomes a non-daemon or a user thread.
Let's see a couple of more differences between the user and daemon thread in Java.
1. JVM doesn't wait for the daemon thread to finish
The first and foremost difference is that JVM will not wait for the daemon thread to finish its work but it will wait for any active user thread. You might have noticed this behavior while running the Java program in Eclipse that even if your main thread has finished the top right button is still red, showing that the Java program is still running. This is due to any user thread spawned from the main thread, but with the main thread, you don't see that red dot in Eclipse.Here is a code snippet from Eclipse which demonstrates that:
2. JVM itself creates a Daemon thread
Another difference between them is that the daemon thread is mostly created by JVM e.g. for some garbage collection jobs. On the other hand, a 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 the shutdown.3. Daemon thread is not used for the critical task
Another key difference between daemons and user thread is that daemons are not used for any critical task. Any important task is done by a 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 threads, the daemon threads are low priority threads. This means they won't get CPU as easily as a user thread can get. See these Java Multithreading Books to learn more about Thread priority and how CPU is allocated between threads.5. JVM closes daemon thread
The user thread is closed by application or by itself but JVM will force the 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 a 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 a 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: t1.setDaemon(true); t1.start(); // let's wait for T1 to finish t1.join(); } private static class Task implements Runnable { @Override 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()); } } } Output 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 a daemon and a non-daemon thread in Java. One of the important things to remember that JVM will forcefully terminate all the active daemon threads if there is no user thread that is pending execution or active, so don't perform any critical task on the daemon thread.
Also, remember, that thread inherits 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)
Thanks for reading this article so far. If you like this interview question and my explanation then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
2) JVM itself creates Daemon thrad -> correct it is thread not thrad ,'e' is missing but your notes is the best i never ever seen like this (feeling proud for you )
ReplyDeleteThanks @Unknown for your kind words and pointing this typo. I'll correct it now.
Delete