Signup/Sign In

Java Daemon Thread

Daemon threads is a low priority thread that provide supports to user threads. These threads can be user defined and system defined as well. Garbage collection thread is one of the system generated daemon thread that runs in background. These threads run in the background to perform tasks such as garbage collection. Daemon thread does allow JVM from existing until all the threads finish their execution. When a JVM founds daemon threads it terminates the thread and then shutdown itself, it does not care Daemon thread whether it is running or not.

daemon-thread

Following are the methods in Daemon Thread

1. void setDaemon(boolean status)

In Java, this method is used to create the current thread as a daemon thread or user thread. If there is a user thread as obj1 then obj1.setDaemon(true) will make it a Daemon thread and if there is a Daemon thread obj2 then calling obj2.setDaemon(false) will make it a user thread.

Syntax:

public final void setDaemon(boolean on)

2. boolean isDaemon()

In Java, this method is used to check whether the current thread is a daemon or not. It returns true if the thread is Daemon otherwise it returns false.

Syntax:

public final booleanisDaemon()

Example:

Lets create an example to create daemon and user threads. To create daemon thread setdaemon() method is used. It takes boolean value either true or false.

	
public class DaemonDemo1 extends Thread 
{ 
    public DaemonDemo1(String name1)
    { 
        super(name1); 
    } 
    public void run() 
    {  
        if(Thread.currentThread().isDaemon()) 
        {  
            System.out.println(getName() + " is Daemon thread");  
        }  
        else
        {  
            System.out.println(getName() + " is User thread");  
        }  
    }  
    public static void main(String[] args) 
    {  
        DaemonDemo1 D1 = new DaemonDemo1("D1"); 
        DaemonDemo1 D2 = new DaemonDemo1("D2"); 
        DaemonDemo1 D3 = new DaemonDemo1("D3"); 

        D1.setDaemon(true);      
        D1.start();  
        D2.start(); 
        D3.setDaemon(true);  
        D3.start();         
    }  
}
	

deamon-thread-example

Example : Daemon thread Priority

Since daemon threads are low level threads then lets check the priority of these threads. The priority we are getting is set by the JVM.

    
public class DaemonDemo1 extends Thread 
{ 
    public DaemonDemo1(String name1)
    { 
        super(name1); 
    } 
    public void run() 
    {  
        if(Thread.currentThread().isDaemon()) 
        {  
            System.out.println(getName() + " is Daemon thread");
        
        }  
        else
        {  
            System.out.println(getName() + " is User thread");  
        }
        System.out.println(getName()+" priority "+Thread.currentThread().getPriority());
    }  
    public static void main(String[] args) 
    {  
        DaemonDemo1 D1 = new DaemonDemo1("D1"); 
        DaemonDemo1 D2 = new DaemonDemo1("D2"); 
        DaemonDemo1 D3 = new DaemonDemo1("D3"); 

        D1.setDaemon(true);   
        D1.start();  
        D2.start(); 
        D3.setDaemon(true);  
        D3.start();         
    }  
}
    

D1 is Daemon thread D1 priority 5 D2 is User thread D3 is Daemon thread D2 priority 5 D3 priority 5

Example

While creating daemon thread make sure the setDaemon() is called before starting of the thread. Calling it after starting of thread will throw an exception and terminate the program execution.

    
public class DaemonDemo1 extends Thread 
{ 
    public DaemonDemo1(String name1)
    { 
        super(name1); 
    } 
    public void run() 
    {  
        if(Thread.currentThread().isDaemon()) 
        {  
            System.out.println(getName() + " is Daemon thread");
        
        }  
        else
        {  
            System.out.println(getName() + " is User thread");  
        }
        System.out.println(getName()+" priority "+Thread.currentThread().getPriority());
    }  
    public static void main(String[] args) 
    {  
        DaemonDemo1 D1 = new DaemonDemo1("D1"); 
        DaemonDemo1 D2 = new DaemonDemo1("D2"); 
        DaemonDemo1 D3 = new DaemonDemo1("D3"); 

        D1.setDaemon(true);   
        D1.start();  
        D2.start();
        D3.start();
        D3.setDaemon(true);  
                 
    }  
}
    

D1 is Daemon threadException in thread "main" D1 priority 5 D3 is User thread D2 is User thread D2 priority 5java.lang.IllegalThreadStateException D3 priority 5 at java.base/java.lang.Thread.setDaemon(Thread.java:1410) at myjavaproject.DaemonDemo1.main(DaemonDemo1.java:32)