Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Daemon Thread in Java: Know what are it’s methods

Last updated on Nov 26,2019 2.6K Views

Aayushi Johari
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...
70 / 72 Blog from Java Core Concepts

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. :-)

Java - daemon thread in Java - Edureka

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 ThreadsUser 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:

MethodsDescription
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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Daemon Thread in Java: Know what are it’s methods

edureka.co