Difference Between Daemon Threads and User Threads In Java
Last Updated :
21 Feb, 2023
In Java, there are two types of threads:
- Daemon Thread
- User Thread
Daemon threads are low priority threads which always run in background and user threads are high priority threads which always run in foreground. User Thread or Non-Daemon are designed to do specific or complex task where as daemon threads are used to perform supporting tasks.
Difference Between Daemon Threads And User Threads In Java
- JVM doesn't wait for daemon thread to finish but it waits for User Thread : First and foremost difference between daemon and user threads is that JVM will not wait for daemon thread to finish its task but it will wait for any active user thread. For example, one might have noticed this behavior while running Java program in NetBeans that even if the main thread has finished, the top left down 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 thread one don't see that red dot in NetBeans.

- Thread Priority : The User threads are high priority as compare to daemon thread means they won't get CPU as easily as a user thread can get.
- Creation of Thread : User thread is usually created by the application for executing some task concurrently. On the other hand, daemon thread is mostly created by JVM like for some garbage collection job.
- Termination of Thread : JVM will force daemon thread to terminate if all user threads have finished their execution but The user thread is closed by application or by itself. A user thread can keep running by the JVM running but a daemon thread cannot keep running by the JVM. This is the most critical difference between user thread and daemon thread.
- Usage : The daemons threads are not used for any critical task. Any important task is done by user thread. A daemon thread is generally used for some background tasks which are not critical task.
The Major Difference between User and Daemon Threads:
User Thread | Daemon Thread |
---|
JVM wait until user threads to finish their work. It never exit until all user threads finish their work. | The JVM will't wait for daemon threads to finish their work. The JVM will exit as soon as all user threads finish their work. |
JVM will not force to user threads for terminating, so JVM will wait for user threads to terminate themselves. | If all user threads have finished their work JVM will force the daemon threads to terminate |
User threads are created by the application. | Mostly Daemon threads created by the JVM. |
Mainly user threads are designed to do some specific task. | Daemon threads are design as to support the user threads. |
User threads are foreground threads. | Daemon threads are background threads. |
User threads are high priority threads. | Daemon threads are low priority threads. |
Its life independent. | Its life depends on user threads. |
Example: Check Thread is Daemon or not One can make a user thread as daemon thread by using setDaemon(boolean) method. In this example, thread type is being checked (User thread or Daemon thread) by using isDaemon() method. It returns true if it is daemon otherwise it returns false.
JAVA
// Java program check thread is Daemon or not
class MyThread extends Thread {
@Override
public void run()
{
System.out.println("User Thread or Non-Daemon Thread");
}
}
class MainThread {
public static void main(String[] args)
{
MyThread mt = new MyThread();
mt.start();
System.out.println("Main Thread");
System.out.println("Is " + mt.getName()
+ " a Daemon Thread: "
+ mt.isDaemon());
System.out.println("Is " + Thread.currentThread().getName()
+ " a Daemon Thread: "
+ Thread.currentThread().isDaemon());
}
}
Output:Main Thread
Is Thread-0 a Daemon Thread: false
Is main a Daemon Thread: false
User Thread or Non-Daemon Thread
Example: Make Non-Daemon Thread as a Daemon Thread: In this example, a non-daemon thread is made a daemon using setDeamon(boolean).
JAVA
// Java program make user thread as a daemon thread
class MyThread extends Thread {
@Override
public void run()
{
System.out.println("Non-Daemon thread");
}
}
class MainThread {
public static void main(String[] args)
{
MyThread mt = new MyThread();
System.out.println("Before using setDaemon() method: ");
System.out.println("Is " + mt.getName()
+ " a Daemon Thread: "
+ mt.isDaemon());
mt.setDaemon(true);
System.out.println("After using setDaemon() method: ");
System.out.println("Is " + mt.getName()
+ " a Daemon Thread: "
+ mt.isDaemon());
}
}
Output:Before using setDaemon() method:
Is Thread-0 a Daemon Thread: false
After using setDaemon() method:
Is Thread-0 a Daemon Thread: true
Similar Reads
Difference Between Java Threads and OS Threads In modern computing, multitasking and parallel processing are essential for improving system performance. Two important concepts that enable this are Java Threads and Operating System (OS) Threads. Both play a key role in managing concurrent tasks, but they operate at different levels and interact w
8 min read
Difference between User Level thread and Kernel Level thread User-level threads are threads that are managed entirely by the user-level thread library, without any direct intervention from the operating system's kernel, whereas, Kernel-level threads are threads that are managed directly by the operating system's kernel. In this article, we will see the overvi
5 min read
Difference between Process and User Level Thread When you open a program like a browser or a game, computer divides its workload into smaller units which are processes and threads. It breaks the work into smaller parts to manage it better. Both play different roles in how the system handles tasks. A process is like a full program with its own memo
3 min read
Difference Between Thread ID and Thread Handle Prerequisite: Thread in Operating System Thread Id is a long positive integer that is created when the thread was created. During the entire lifecycle of a thread, the thread ID is unique and remains unchanged. It can be reused when the thread is terminated. The Thread get_id() method can be studied
3 min read
Difference between Zombie, Orphan and Daemon Processes In a computer, processes are the fundamental units of execution for any program or task. Every process behaves differently after its primary tasks are completed or if their parent processes are terminated. Understanding the differences between zombie, orphan, and daemon processes is very important f
4 min read