isDaemon()
is used to check whether a thread is a daemon thread.
Note:
isDaemon()
is a thread class method.
Daemon Thread:
What is Daemon Thread?
final boolean isDaemon()
This method does not take any argument value.
booleanValue
: It returns True
, if the thread is daemon. Otherwise, it returns False
.The code snippet below elaborates the use of isDaemon()
method in a program:
// sample class implementing Runnable interface.public class SampleThread implements Runnable {// run method for program executionpublic void run() {if(Thread.currentThread().isDaemon())System.out.println(Thread.currentThread().getName() + " is a daemon thread");elseSystem.out.println(Thread.currentThread().getName() +" is a user thread");}}
SampleThread
instance sample
because it implements a Runnable interface.thread1
with sample
and thread “Thread 1
”.thread2
with sample
and thread “Thread 2
” to the constructor.Thread 1
as daemon thread i.e. thread1.setDaemon(true)
.thread2.Start()
begins execution of thread2
by calling JVM to call run()
method from Runnable interface.thread1.Start()
begins execution of thread1
by calling JVM to call run()
method from Runnable interface.Thread 1 is a daemon thread
".Thread 2 is a user thread
".