Daemon thread in java is a thread that has the lowest priority and is used to provide services to the thread, which is further used for performing the background tasks. Performing tasks in the background include garbage collection and many more tasks that involve JVM to prevent itself at the time of execution simultaneously after exiting. Java virtual machine terminates itself on finding other user threads which are not actually the daemon threads. In case Java Virtual Machine finds Daemon thread, then it will terminate that thread first and then it will itself get shutdown.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There is no specific syntax for the Daemon Thread, but it can be created and called by other methods as part of the Daemon Thread, which is represented as :
public class DaemonThread extends Thread
Further, it involves various methods that have different syntax for calling the Daemon thread.
public final void setDaemon(boolean on)
This method syntax passes the parameter as boolean on which is used for because the thread is a daemon thread when daemon thread is marked as boolean on which means true.
public final boolean isDaemon()
This method returns the value as true if the thread found is daemon thread and false if the thread found is false.
A daemon thread is a thread used to create a thread and inherits all the properties from its parent thread. Therefore, all the threads which are present within the main method are called the child method, which inherits most of the properties for execution and performing any task from the parent class. They are also called a non-daemon thread.
Here comes the real picture of making the non-daemon threads or user threads as daemon threads by calling all the methods for setting the thread as Daemon threads. It can easily be set as a Daemon thread using the setDaemon() method of the Thread class, which is the class from which the runnable interface also gets extended.
One very important point to keep in mind is that whenever a JVM gets started, then at that time, a thread called main gets created, and any program gets run or started at this point, or we can say that this is the point where the flow of execution gets started. All thread, by default, will run on this main thread until a new thread is initiated. In case a new thread gets initiated, then the child thread will be responsible for creating any new method.
Then comes methods responsible for creating and determining daemon threads, which involves methods like the setDaemon method, which will determine whether the thread created is daemon thread or non-daemon thread based on the value of the parameter passed to the method.
Public boolean thread isDaemon() is used on the other hand to check the status of the thread based on the return type; if false, then the thread is non-daemon; otherwise, it is daemon.
Here are the methods mention below
This method is used to make the current thread execution into daemon thread based on the parameter passed if it is boolean on, then the thread is Daemon thread if the parameter passed is not boolean, then the thread is the non-daemon thread.
It also catches holds exceptions which are like IllegalThreadStateException or Security Exception. If it is determined to catch illegalThreadStateException, then it is a pre-requisite that the thread must be active only. If the current thread cannot be modified, then it will use Security Exception.
This method is used to verify the present situation or say the current status based on the return type; if the return type is true, then it is a daemon thread; otherwise, it is the non-daemon thread.
Here are the following examples mention below:
This program demonstrates the method of setting the thread as daemon thread for further manipulation using the void setDaemon(boolean status), and isDaemon() method once called after identifying the Daemon thread, which represents whether the thread set is daemon thread or the non-daemon thread depending on the parameter set with boolean value for the parameter.
Code:
public class Daemon_Thread_Demo_set extends Thread { public Daemon_Thread_Demo_set(String nm){ super(nm); } public void run() { if(Thread.currentThread().isDaemon()) { System.out.println("Current_Thread is a Daemon thread" + Thread.currentThread().getName()); } else { System.out.println("Current thread is a non-daemon thread. "+ Thread.currentThread().getName()); } } public static void main(String[] args) { Daemon_Thread_Demo_set nm1 = new Daemon_Thread_Demo_set("nm1"); Daemon_Thread_Demo_set nm2 = new Daemon_Thread_Demo_set("nm2"); Daemon_Thread_Demo_set nm3 = new Daemon_Thread_Demo_set("nm3"); nm1.setDaemon(true); nm1.start(); nm2.start(); nm3.setDaemon(false); nm3.start(); } }
Output:
This program demonstrates the illegal exceptions which are thrown once the current thread is identified as the Daemon thread but then the value when the set is not coming out to be set value matched with the thread it will be throwing the illegal exception as represented in the output.
Code:
public class Daemon_thrd_demo2 extends Thread { public void run() { System.out.println("Current_thread_Name: "+Thread.currentThread().getName()); System.out.println("thread_as_Daemon"+Thread.currentThread().isDaemon()); } public static void main(String[] args) { Daemon_thrd_demo2 tst1 = new Daemon_thrd_demo2(); Daemon_thrd_demo2 tst2 = new Daemon_thrd_demo2(); tst1.start(); tst1.setDaemon(true); tst2.start(); } }
Output:
Note: The above program gives the Exception because the user thread should be set as a daemon thread only before calling the setDaemon() method. If it is not called in this way, then the thread after invocation will throw these exceptions while execution. Also, all non-daemon thread and daemon thread should be set up keeping in mind the place, i.e. before the child class or before the parent class.Daemon thread and non-daemon thread have differences in them. It is very important to identify these differences as it can lead to conflicts and exception because daemon thread is a kind of thread that has the lowest priority compared to other threads while execution.
The above is the detailed content of Daemon Thread in Java. For more information, please follow other related articles on the PHP Chinese website!