Pages

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:

2) JVM itself creates Daemon thread
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.

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.

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.

Thanks for reading this article so far. If you like this interview question and my explanation then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.