Home »
Java programming language
Java Thread Class final boolean isDaemon() method with Example
Java Thread Class final boolean isDaemon() method: Here, we are going to learn about the final boolean isDaemon() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 25, 2019
Thread Class final boolean isDaemon()
- This method is available in package java.lang.Thread.isDaemon().
- This method is used to check whether the current thread is a daemon thread.
- Daemon thread is the thread which runs in the background.
- This method is not static so we cannot access this method with the class name too.
- This method is final we can't override this method in child class.
- The return type of this method is boolean so it returns true if the thread is daemon else return false if the thread is user thread.
Syntax:
final boolean isDaemon(){
}
Parameter(s):
We don't pass any object as a parameter in the method of the Thread.
Return value:
The return type of this method is boolean, it returns true if this thread is daemon else return false.
Java program to demonstrate example of isDaemon() method
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class IsThreadDaemon extends Thread {
//Override run() method of Thread class
public void run() {
// Code to check for daemon thread
if (Thread.currentThread().isDaemon()) {
//Display Daemon thread code
System.out.println("Is thread " + getName() + "daemon?" + Thread.currentThread().isDaemon());
} else {
System.out.println("Not a Daemon thread" + getName());
}
}
public static void main(String[] args) {
// creating three object of the class IsThreadDaemon
IsThreadDaemon td1 = new IsThreadDaemon();
IsThreadDaemon td2 = new IsThreadDaemon();
IsThreadDaemon td3 = new IsThreadDaemon();
// td2 is a daemon thread which is set by setDaemon(true) method
td2.setDaemon(true);
// By using start() method we will start execution of the thread
td1.start();
td2.start();
td3.start();
}
}
Output
E:\Programs>javac IsThreadDaemon.java
E:\Programs>java IsThreadDaemon
Not a Daemon threadThread-0
Not a Daemon threadThread-2
Is thread Thread-1daemon?true