Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Daemon Thread in Java

Last Updated on March 31, 2023 by Prepbytes

In modern-day programming, Multithreading is an important concept and Java provides various tools to implement the concept of multi-threading in applications. The threads in Java are classified into two categories namely, User Threads and Daemon Threads in Java. here we will discuss the concept of Daemon Thread in Java and its properties and uses.

What is Daemon Thread in Java?

Daemon Thread in Java is a special type of thread that runs in the background and provides support to the other threads present in the applications. Daemon Thread in Java is known as the background thread as they run continuously in the background until the application terminates. The Daemon Threads in Java are designed to support the application’s used threads.

The Daemon Thread in Java is given the lowest possible priority and the JVM terminates them automatically when all the user threads have terminated.

Properties of Daemon Thread in Java

The following are some properties of Daemon Thread in Java.

  • It is the lowest-priority thread.
  • They are automatically terminated by the JVM if all other used threads have been terminated.
  • Daemon Thread in Java cannot prevent the JVM from terminating if all other threads have completed their tasks.
  • The JVM does not know the status of the Daemon Thread in Java whether it is active or not.

Now before understanding how to create a Daemon Thread in Java, let’s first discuss the methods for Daemon Thread in Java.

Methods Present for Daemon Thread in Java

The following methods are defined in the Thread Class for Daemon Thread in Java.

  • public void setDaemon(boolean status)
    This method is used for declaring the current thread as a daemon thread or a user thread.

  • public final boolean isDaemon()
    This method returns a boolean value whether the current thread is a Daemon Thread or not.

Example of Daemon Thread in Java

A Daemon Thread in Java is created by implementing the Runnable Interface and passing it to the Thread constructor. For creating a Daemon Thread in Java, we use the setDaemon() method to set the thread’s daemon status before starting the thread. The following code demonstrates the Creation of a Daemon Thread in Java.

Code:

class DaemonThread1 extends Thread{ 
   public void run(){ 
        if(Thread.currentThread().isDaemon()){
            System.out.println("Daemon Thread executing"); 
        } else { 
            System.out.println("User thread excuting"); 
        } 
    } 
    public static void main(String[] args){ 
        DaemonThread1 t1 = new DaemonThread1();
        DaemonThread1 t2 = new DaemonThread1(); 
        DaemonThread1 t3 = new DaemonThread1(); 
 
        t1.setDaemon(true);
 
        t1.start();
        t2.start(); 
        t3.start(); 
    } 
} 

Output:

Daemon Thread executing
User thread excuting
User thread excuting

Explanation:
In the above code, we have created a class named “DaemonThread1” that implements the Runnable interface. The run() method contains a code that checks whether the current thread is a Daemon thread or not and prints the statement accordingly.

In the main() method, we created three instances of the class “DaemonThread1”. We then set the Daemon status of the Thread t1 using the setDaemon() method and then we started all threads by using the start() method.

Uses of Daemon Thread in Java

Here are some common uses of the Daemon Thread in Java.

  • The Daemon Thread in Java is quite useful in situations where we need to perform the background processes in a continuous manner.
  • Daemon Thread in Java is also used when an application need to perform maintenance tasks in the background.
  • Daemon threads are also used in some standard Java Classes.

Exceptions in Daemon Thread in Java

The Daemon Thread in Java, if not handled carefully throws exceptions. The following Exceptions are thrown if Daemon Thread is not used in the correct manner.

  • IllegalThreadStateException: This exception is thrown if you call the setDaemon() method on a thread that has already been started.
  • SecurityException: This exception is thrown when the current thread is not able to change the thread.

Difference between User Thread and Daemon Thread in Java

The difference between the User Thread and Daemon Thread in Java are listed in the table given below.

Daemon Thread User Thread
These are low-priority threads. These have higher priority.
Life is dependent on the User Threads. Independent of the life of daemon thread.
Used for background tasks. Used for executing the main tasks of the application.

Conclusion
In this article, we have learned about the Daemon Thread in Java. Daemon Thread in Java is a useful tool for implementing the background tasks which help in supporting the application’s user thread. We can set the Daemon Status of a thread using the setDaemon() method. Also, to check whether a thread is a Daemon Thread in java with the help of the isDaemon() method.

Frequently Asked Questions (FAQs)

Here are some Frequently Asked Questions related to “Daemon Thread in Java”.

Ques 1. Can a daemon thread prevent an application from terminating?
Ans. No, a daemon thread cannot prevent an application from terminating as it is designed to support user threads and does not prevent the application from terminating.

Ques 2. What happens to a daemon thread when all user threads have terminated?
Ans. When all user threads have terminated, the JVM terminates any remaining daemon threads automatically.

Ques 3. Can a daemon thread have a higher priority than a user thread?
Ans. No, a daemon thread has a lower priority than a user thread.

Ques 4. Can you change a thread’s daemon status after it has been started?
Ans. No, you cannot change a thread’s daemon status after it has been started.

Ques 5. How can you identify a daemon thread in Java?
Ans. You can identify a daemon thread in Java by calling the isDaemon() method on the thread object.

Ques 6. Can you create multiple daemon threads in a Java application?
Ans. Yes, you can create multiple daemon threads in a Java application.

Leave a Reply

Your email address will not be published. Required fields are marked *