A thread is a lightweight process. Threads reduce inefficiency by preventing the waste of CPU cycles. Java, being a popular and easy programming language, provides built-in support to Multithreading. Every thread has its priority and the one with higher priority tends to execute faster. Unlike other threads, daemon threads in Java is a low priority thread which runs in the background.
This blog will introduce you to Java Daemon threads in the following order.
Let’s get started. :-)
What is a Daemon Thread in Java?
Daemon thread in Java provides service to the user thread which runs in the background. It is considered to be a low priority thread which is used to perform tasks such as garbage collection. In java, every thread has its priority and the one with higher priority tends to execute faster. Also, Java Virtual Machine(JVM) terminates this thread automatically. It can not prevent JVM from exiting when all the user threads finish their execution, even if daemon thread itself is running.
Moving ahead, let’s see how daemon threads are different from user threads (non-daemon).
Daemon Thread vs User Threads
The major difference between a daemon thread and user thread is because of JVM. As discussed above, Java Virtual Machine does not wait for a daemon thread to finish its execution while it waits for user thread to finish. Let’s explore some more differences between Daemon thread and user thread with the help of a below table:
Daemon Threads | User Threads (Non-daemon) |
Daemon threads are created by JVM | User threads are created by an application itself |
JVM does not wait for its execution | JVM waits until the execution completes |
Low Priority threads | High priority threads |
Used for background tasks(not critical) | Used for foreground tasks(critical) |
Life is dependent on user threads | Life is independent |
Now that you are clear with the difference between daemon vs user threads, let us look at an example program to check whether a thread is a daemon or non-daemon thread.
public class ExampleThread extends Thread { @Override public void run() { System.out.println("User Thread or Non-Daemon Thread"); } public static void main(String[] args) { ExampleThread obj = new ExampleThread(); obj.start(); System.out.println("is " + obj.getName() + " a Daemon Thread: " + obj.isDaemon()); System.out.println("is " + Thread.currentThread().getName() + " a Daemon Thread: " + Thread.currentThread().isDaemon()); } }
Output: is Thread-0 a Daemon Thread: false
User Thread or Non-Daemon Thread
is main a Daemon Thread: false
Moving ahead, let’s see different methods in daemon thread in Java.
Methods in Java Daemon Thread
There are two main methods for a daemon thread in Java, namely:
Methods | Description |
public void setDaemon(boolean status) | Marks this thread as either a daemon thread or a user thread(non-daemon thread). |
public boolean isDaemon() | Used to test if this thread is a daemon thread or not. Returns true if the thread is Daemon else false. |
Consider the below code for practical implementation:
public class Demothread extends Thread { // Java program to demonstrate the usage of // setDaemon() and isDaemon() method. public Demothread(String name){ super(name); } public void run() { // Checking whether the thread is Daemon or not if(Thread.currentThread().isDaemon()) { System.out.println(getName() + " is Daemon thread"); } else { System.out.println(getName() + " is User thread"); } } public static void main(String[] args) { Demothread thread1 = new Demothread("thread1"); Demothread thread2 = new Demothread("thread2"); Demothread thread3 = new Demothread("thread3"); // Setting user thread thread1 to Daemon thread1.setDaemon(true); // starting first 2 threads thread1.start(); thread2.start(); // Setting user thread thread3 to Daemon thread3.setDaemon(true); thread3.start(); } }
Output:
thread2 is User thread
thread1 is Daemon thread
This is the end of the “Daemon thread in Java” blog. I hope you guys are clear with the content I have discussed above. Do read my next blog on Java Interview Questions where I have listed top 75 interview questions and answers which will help you set apart in the interview process.
Now that you have understood Java Collections, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Daemon thread in Java” blog and we will get back to you as soon as possible.