Daemon threads in java

Daemon threads

A daemon thread is a thread that executes continuously. Daemon threads are service providers for other threads or objects.
To make a thread t as a daemon thread, we can use setDaemon() method as:
t.setDaemon(true);
To know if a thread is daemon or not, isDaemon() is useful.
Boolean x = t.isDaemon();

Example
public class test extends Thread
{
public void run()
{
if(Thread.currentThread().isDaemon())
{
System.out.println(“The daemon thread is working”);
}
else
{
System.out.println(“The user thread is working”);
}
}
public static void main(String[] args)
{
test t1=new test();
test t2=new test();
test t3=new test();
t1.setDaemon(true);
t1.start();
t2.start();
t3.start();
}
}

Output:
The user thread is working
The daemon thread is working
The user thread is working

Leave a Reply

Your email address will not be published. Required fields are marked *