Daemon Thread in Java
A daemon is a special kind of thread in the Multithreaded Programming Environment that operates quietly and handles routine tasks that are not intrinsically significant to the program’s core function.
Unlike regular threads, daemon threads have a unique feature: they automatically stop when the main program or all other non-daemon threads have finished running.
This property allows them to be useful in managing tasks such as regular maintenance, tracking and complementary processes that do not extend a program’s life cycle.
Some of the basic characteristics of a daemon thread are given below
Background tasks: Daemon threads are usually used to accomplish tasks unimportant for the program’s main functionality.
The threads are often engaged in monthly cleaning, observation or auxiliary processing tasks.
Termination: When all non-daemon threads have completed their work and the main program exits, daemon threads are abruptly terminated, even if they are in the middle of executing a task. This ensures that they don’t accidentally run the program.
A SetDaemon method: You can set a special property or flag to designate the thread as some slave by using many programming languages and threads libraries. For example, before starting a thread, you can use the setDaemon()+True method in Java to mark it as a daemon thread.
Here’s an example in Java of how to create a daemon thread:
public class FirstCode{ public static void main(String[] args) { Thread daemonThread = new Thread(() -> { while (true) { System.out.println("Daemon thread is running."); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); daemonThread.setDaemon(true); daemonThread.start(); System.out.println("Main thread is exiting."); } }
Output:
Main thread is existing.
Daemon thread is running.
The Java Daemon thread properties
No Preventing JVM Exit
When all user threads are finished executing, daemon threads cannot stop the JVM from leaving. The JVM will terminate itself when all user threads complete their task, regardless of whether the thread daemon is running.
Automatic termination
When a running daemon thread is detected, the JVM shall terminate and close it.
Low priority
The Daemen are the least important thread in Java.
The default characteristics of a Daemon thread
As a rule, the main thread is always a non-daemon thread. However, their daemonic nature is derived from a parent thread for all other threads.
If the parent thread is a daemon, the child thread is also a daemon, and if the parent thread is not a daemon, the child thread is also not a daemon.
Note: All daemon threads shall be stopped automatically when the last nondestructive thread ends.
Methods of Daemon Thread
void setDaemon(boolean status)
This method shall identify the current thread as a daemon or user thread. You can set a user thread to daemon status using the ‘tU.setDaemon(true)’, while you can set a User thread to Daemon Status using the ‘tD.setDaemon(false)’.
Syntax:
public final void setDaemon(boolean on)
- Parameters:
If it turns out to be true, assign this link as a daemon thread.
- Exceptions:
The IllegalThreadStateException: unless this thread is active. SecurityException: if the current thread cannot modify this thread.
boolean isDaemon()+ To verify that the currently active thread is a daemon, use this method. If the thread’s Daemon, it’ll be true. Otherwise it’s going to come back as incorrect.
Syntax:
public final boolean isDaemon()
Daemon vs. User Threads
Priority
The JVM exits when it’s the only daemon thread remaining in a process. This is understandable since the daemon thread does not need to provide services to another thread when all of them are running.
Usage
Deemed threads are mainly used to provide background support for user threads. When performing tasks that support the main operation, they do not interfere with the user’s operations.
Understanding the Daemon threads is necessary for Java developers to optimise application performance and efficiently manage thread behavior.
In Java, the following data are to be remembered for Daemon Thread
- It provides background support tasks and services to user threads.
- Serving user threads does not have a role in life.
- It is dependent upon user threads for its life. It is a low priority thread.
Why does JVM turn off the Domains thread if you don’t have user threads?
A daemon thread is solely intended to provide background support services to user threads.
Why should the JVM run this thread if there’s no user thread?
It’s the reason JVM stops daemon threads when there isn’t a user thread.
Method to thread a Java Daemon by the Thread class
The java.lang. There are two Java daemon thread methods within the THREAD class.
S.No | Method | Description |
1. | public void setDaemon(boolean status) | This is to designate the existing thread as a daemon or user thread. |
2. | public boolean isDaemon() | This method is used to check if the current thread is daemon. |
This is to designate the existing thread as a daemon or user thread. It is used to check that the current thread is a daemon.
Example of Daemon thread in Java:
public class FirstCode extends Thread{ public void run(){ if(Thread.currentThread().isDaemon()) { System.out.println("daemon thread work"); } else{ System.out.println("user thread work"); } } public static void main(String[] args){ FirstCode t1=new FirstCode(); FirstCode t2=new FirstCode(); FirstCode t3=new FirstCode(); t1.setDaemon(true); t1.start(); t2.start(); t3.start(); } }
Output:
daemon thread work
user thread work
User thread work
Conclusion
Lastly, the thread daemon’s important role is to perform routine tasks that are not essential for a program’s core function by enabling them to be performed as part of Multithreaded Programming.
Thanks to their key features, including automatic termination when the main program or all other non-daemon threads are complete, they are valuable for managing auxiliary processes without prolonging the program’s life cycle.