Daemon Thread in Java

Daemon Threads are the service provider threads which provides services to the non-daemon threads(user thread). Daemon threads and non-daemon threads are much similar to each other , the only difference in between them is in their existence.That means existence of Daemon threads depend upon non-daemon threads.

Daemon thread in java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically.
There are many java daemon threads running automatically e.g. gc, finalizer etc.


Methods used for Performing operation with Daemon Threads

In Java each thread has a Daemon flag value associated with it . By default the value of daemon flag is set to false .
The java.lang.Thread class provides two methods for java daemon thread.

Method Description
public void setDaemon() It is used to set the boolean flag value of Daemon thread to create Daemon or non-daemon thread.
public boolean isDaemon is used to check that current is daemon or not.

 

Whenever we want to create any Daemon thread , we must call setDaemon(boolean) just before allocating resource to the thread( calling start() method ).

Simple example of Daemon thread in java

public class TestDaemonThread1 extends Thread{
 public void run(){
  if(Thread.currentThread().isDaemon()){//checking for daemon thread
   System.out.println("daemon thread work");
  }
  else{
  System.out.println("user thread work");
 }
 }
 public static void main(String[] args){
  TestDaemonThread1 t1=new TestDaemonThread1();//creating thread
  TestDaemonThread1 t2=new TestDaemonThread1();
  TestDaemonThread1 t3=new TestDaemonThread1();

  t1.setDaemon(true);//now t1 is daemon thread
  
  t1.start();//starting threads
  t2.start();
  t3.start();
 }
}
daemon thread work
user thread work
user thread work

Another example of Daemon thread


//program for performing operation with Deamon thread
class UserThread extends Thread
{
  public void run()
  {
  	Thread t=Thread.currentThread();
  	System.out.println(t.getName()+ " started.....");
  	 if(t.isDaemon())
  	 {
  	 	t.setName("Daemon");
  	 	//stuff for Daemon thread
  	 	for(int i=1;i<=1000;i++)
  	 	{
  	 		System.out.println(t.getName() +" : "+i);
  	 		try
  	 		{
  	 			Thread.sleep(1000);
  	 		}catch(Exception e)
  	 		{
  	 			e.printStackTrace();
  	 		}
  	 	}
  	 }else
  	 {
  	 	t.setName("Normal");
  	 	//stuff for Normal thread
  	 	for(int i=1;i<=15;i++)
  	 	{
  	 		System.out.println(t.getName() +" : "+i);
  	 		try
  	 		{
  	 			Thread.sleep(1000);
  	 		}catch(Exception e)
  	 		{
  	 			e.printStackTrace();
  	 		}
  	 	}

  	 }
  	 System.out.println(t.getName()+ " terminated.....");
  }
  public static void main(String[] args) {
  	UserThread t1=new UserThread();
  	UserThread t2=new UserThread();

  	t1.setDaemon(true);//making Daemon thread

  	t1.start();
  	t2.start();
  }
}
Thread-0 started.....
Thread-1 started.....
Daemon : 1
Normal : 1
Normal : 2
Daemon : 2
Daemon : 3
Normal : 3
Normal : 4
Daemon : 4
Normal : 5
Daemon : 5
Normal : 6
Daemon : 6
Daemon : 7
Normal : 7
Normal : 8
Daemon : 8
Daemon : 9
Normal : 9
Normal : 10
Daemon : 10
Normal : 11
Daemon : 11
Normal : 12
Daemon : 12
Normal : 13
Daemon : 13
Normal : 14
Daemon : 14
Normal : 15
Daemon : 15
Normal terminated.....

Garbage Collector

It is Daemon thread started by JRE as a Daemon thread which is used to release the unused memory.