Home >Java >javaTutorial >Daemon Thread in Java

Daemon Thread in Java

WBOY
WBOYOriginal
2024-08-30 16:03:14497browse

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

Syntax

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
  • public class: The access modifier of the class is public and
  • DaemonThread: The type of thread is DaemonThread therefore, it gets inherited and extended from the Thread class.
  • Thread: The thread class is used to inherit the Thread class’s properties to the Daemon 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.

How does Daemon Thread work in Java?

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.

Methods

Here are the methods mention below

1. public void setDaemon(boolean status)

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.

2. public boolean isDaemon()

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.

Examples of Daemon Thread in Java

Here are the following examples mention below:

Example #1

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:

Daemon Thread in Java

Example #2

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:

Daemon Thread in Java

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.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java thread dumpNext article:Java thread dump