by Dinesh Thakur

Java has two types of threads : user thread and daemon thread.

A user thread is a thread that will run on its own independently of other threads. In other words, a user thread has life of its own that is not dependent on the thread that creates it. It can continue execution, even after the end of the thread that created it. One example of user thread is the main thread which runs your main () thread. On the other hand, a daemon thread is subordinate to a user thread i.e. a daemon thread will automatically terminate when no more user threads are running in the application.

Daemon threads are often used to provide some service that is used by one or more user threads. These threads run in the background continuously until the application ends. In other words, these threads do not need any explicit termination condition to end them because JVM automatically ends them with the termination of user thread. An example of daemon thread is the thread that performs garbage collection.

When a thread is created, a Thread instance will be of the same type as it creating thread. In examples discussed so far, the threads created in the main() thread were all user threads because they are created by main thread which is user thread. However, in order to change a user thread to a daemon thread, call setDaemon() method of the Thread object, which takes a boolean argument. Its syntax is

final void setDaemon(boolean value)

If the value is true then the thread will be a daemon thread, otherwise if it is false, it will be a user thread. Note that the setDaemon () needs to be called before the thread is started (i.e. before invoking the start () method). If you try to do so afterwards then the method will throw an IllegalThreadStateException exception.

You can also determine if a thread is daemon thread or a user thread. For this, call the isDaemon () method of the thread object. Its syntax is

final boolean isDaemon()

This method will return true if invoking thread is a daemon thread, otherwise return false.

Now let us consider a program,

class MyThread extends Thread

{
     private int threadNum;
     MyThread(int n) { threadNum = n;}
     public void run()
     {
      for(int i=0; i<5;i++)
          {
                try
                {
                      Thread.sleep(500);
                }
                      catch(InterruptedException e){}
                      System.out.println("Thread Number " + threadNum + " is Running");
           }
      }
}
  public class DaemonThread
{
        public static void main(String[] args)
      {
           System.out.println("Main Thread Starts");
           MyThread t1 = new MyThread(1);
           MyThread t2 = new MyThread(2);
           MyThread t3 = new MyThread(3);
           t1.setDaemon(true);
           t2.setDaemon(true);
           t3.setDaemon(true);
           t1.start();
           t2.start();
           t3.start();
           System.out.println("Main Thread Finished");
      }
}

Daemon Threads in Java Examples