Daemon Thread in JAVA

Daemon Thread in JAVA

daemon thread is a threads that runs intermittently in the background as long as other non-daemon threads(user threads) are running. Daemon threads are service providers for user threads running in the same process. Daemon thread in java can be useful to run some tasks in background. Daemon thread is a low priority thread and it does not prevent the JVM from exiting even if the daemon thread itself is running. JVM terminates itself when all user threads (non-daemon threads) finish their execution, JVM is not bothered whether daemon thread is running or not, even if JVM finds daemon thread are yet running but upon completion of user threads, it terminates the program.

An example for a daemon thread is the garbage collection.

Difference between Daemon Threads and Non-Daemon Threads

By default, when a thread is created in JAVA it is a user thread(non-daemon thread). If we want a thread to behave as daemon thread, we need to setDaemon() to true(scroll down to see how to create a daemon thread).

The JVM does not wait for daemon thread before exiting the program, while it waits for user threads(non-daemon threads) until all the user threads finish their execution.

Creation of Daemon Thread

Whenever a thread is created in JAVA, it is by default a user thread(non-daemon thread). We can change from a user thread to a daemon thread by using Thread.setDaemon(true).

Example

In the below program, we will create a daemon thread as well as check whether the thread created is a daemon thread or not by using Thread.isDaemon() method.

Program

class daemonThread implements Runnable
{
    public void run()
    {
        int c = 0;
        while(true)
        {
            System.out.println("Daemon thread running... count = "+(++c));
        }
    }
}
class Main
{
    public static void main(String args[])
    {
        daemonThread dt = new daemonThread();
        Thread t = new Thread(dt); 
        //Changing user thread(t) to daemon thread
        t.setDaemon(true);
        t.start();
        System.out.println("Thread is a DaemonThread = "+t.isDaemon());
    }
}

Output

Thread is a DaemonThread = true
Daemon thread running... count = 1
Daemon thread running... count = 2
Daemon thread running... count = 3
Daemon thread running... count = 4
Daemon thread running... count = 5

In the above program we have created a thread t, which is a user thread. In order to change it from a user thread to a daemon thread we have called t.setDaemon(true). After calling t.start(), we enter the run() method which as infinite loop(while(true)), but as you can see we get a finite output as we know that we also have another thread which is created by default in JAVA called the main thread which is a user thread, so as soon as the main thread finishes its execution the JVM terminates the program and does not wait for the daemon thread(t) to finish its execution which is running endlessly inside the run() method.

Share Me!
  •  
  •  
  •  
  •  
  •