5 - 1 - 8 - Daemon Thread
5 - 1 - 8 - Daemon Thread
1) public void setDaemon(boolean status) is used to mark the current thread as daemon
thread or user thread.
JVM waits for user threads to finish JVM will not wait for daemon threads to
their work. It will not exit until all user finish their work. It will exit as soon as
threads finish their work. all user threads finish their work.
Daemon threads are background
User threads are foreground threads.
threads.
Daemon threads are low priority
User threads are high priority threads.
threads.
User threads are created by the Daemon threads, in most of time, are
application. created by the JVM.
JVM will not force the user threads to JVM will force the daemon threads to
terminate. It will wait for user threads to terminate if all user threads have
terminate themselves. finished their work.
Example
public class TestDaemonThread1 extends Thread output:
{ daemon thread work
user thread work
public void run() user thread work
{
if(Thread.currentThread().isDaemon())
{
System.out.println("daemon thread work");
}
else
{
System.out.println("user thread work");
}
}
public static void main(String[] args)
{
TestDaemonThread1 t1=new TestDaemonThread1();
TestDaemonThread1 t2=new TestDaemonThread1();
TestDaemonThread1 t3=new TestDaemonThread1();
t1.setDaemon(true);
t1.start();
t2.start();
t3.start();
}
}
Example1