Daemon Thread In Java

Multithreading is an essential aspect of Java programming that enables the concurrent execution of multiple tasks, thereby enhancing the overall performance and effectiveness of applications. Daemon threads stand out among different thread types as they serve as background service providers. These threads are considered low-priority and operate in the background to assist user threads in executing primary tasks seamlessly, eliminating the need for direct involvement from the programmer.

A significant feature of a daemon thread is its dependency on user threads for its lifecycle. Once all user threads finish their execution, the Java Virtual Machine (JVM) will terminate all daemon threads automatically, as they lack a purpose in the absence of user processes. Developers have the ability to establish daemon threads by utilizing the setDaemon(boolean) method and can confirm their nature using the isDaemon method.

Important Points to Remember

  • It provides services to user threads for background supporting tasks. It has no role in life than to serve user threads.
  • Its life depends on user threads.
  • It is a low priority thread.
  • Why JVM terminates the daemon thread if there is no user thread?

The main function of a daemon thread is to assist user threads with background tasks. In the absence of user threads, the JVM has no reason to maintain the daemon thread's operation. Consequently, if there are no user threads, the JVM will terminate the daemon thread.

Thread Class Daemon Thread Methods

The Java daemon thread is supported by two methods in the java.lang.Thread class.

No. Method Description
1) public void setDaemon(boolean status) It is used to mark the current thread as daemon thread or user thread.
2) public boolean isDaemon() It is used to check that current is daemon.

Example: Daemon Thread

Example

Example

public class Main extends Thread{   

public void run(){   

  if(Thread.currentThread().isDaemon()){//checking for daemon thread   

   System.out.println("daemon thread work");   

  }   

  else{   

  System.out.println("user thread work");    

}   

}    

public static void main(String[] args){    

  Main t1=new Main();//creating thread   

  Main t2=new Main();   

  Main t3=new Main();        

  t1.setDaemon(true);//now t1 is daemon thread   

  t1.start();//starting threads    

  t2.start();    

  t3.start();   

}   

}

Output:

Output

daemon thread work

user thread work

user thread work

Note: If you want to make a user thread as Daemon, it must not be started otherwise it will throw IllegalThreadStateException. Consider the following example for the same.

Example

Example

class Main extends Thread{  

public void run(){  

  System.out.println("Name: "+Thread.currentThread().getName());  

  System.out.println("Daemon: "+Thread.currentThread().isDaemon());  

}  

public static void main(String[] args){  

  Main t1=new Main();  

  Main t2=new Main();  

  t1.start();  

  t1.setDaemon(true);//will throw exception here  

  t2.start();  

}  

}

Output:

Output

exception in thread main: java.lang.IllegalThreadStateException

Conclusion

Daemon threads in Java are essential for supporting background services for user-defined threads. These threads, known as "service provider" threads, operate discreetly in the background to aid user threads in tasks like garbage collection, memory management, and cleanup operations. Unlike user threads, daemon threads do not handle independent tasks; their primary function is to facilitate the seamless execution of user-defined processes.

Daemon threads are distinct in that they are dependent on user threads for their lifecycle. When all user threads complete their tasks, the Java Virtual Machine (JVM) will promptly terminate any lingering daemon threads since they serve no purpose once user processes are inactive.

Daemon Thread MCQs

  1. Which of the following statements is true about daemon threads in Java?
  • Daemon threads run until the JVM is manually terminated.
  • Daemon threads keep running even after all user threads finish.
  • Daemon threads are automatically terminated when all user threads finish execution.
  • Daemon threads are always high-priority threads.

Explanation: The lifecycle of a daemon thread depends on user threads. When no user thread is alive, JVM automatically kills all daemon threads, as they only provide background services.

  1. Which method is used to check whether a thread is a daemon thread or not?
  • Thread.isAlive
  • Thread.isDaemon
  • Thread.getDaemon
  • Thread.checkDaemon

Explanation: The isDaemon method of the Thread class returns true if the current thread is a daemon thread; otherwise, it returns false.

  1. What happens if you try to call setDaemon(true) on a thread after starting it?
  • It sets the thread as a daemon successfully.
  • The JVM silently ignores the call.
  • It throws IllegalThreadStateException.
  • It converts the thread into a user thread.

Explanation: A thread must be marked as daemon before starting it. If you attempt to set a thread as daemon after calling start, Java throws an IllegalThreadStateException.

  1. Which of the following is an example of a daemon thread in Java?
  • Main thread
  • Garbage Collector thread
  • User-defined worker thread
  • I/O operation thread created by the programmer

Explanation: Garbage Collector (GC), Finalizer, and other system service threads are examples of daemon threads. They run in the background to support user threads. The main thread and user-defined worker threads are not daemon by default.

  1. Which of the following best describes the priority of daemon threads?
  • Always higher than user threads
  • Always lower than user threads
  • Same as user threads but usually assigned lower priority
  • Cannot be changed by the programmer

Daemon threads share the priority range of 1 to 10 with user threads. Nonetheless, they typically operate at a lower priority level as their primary function is to support user threads rather than contend with them.

Input Required

This code uses input(). Please provide values below: