Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection.
Properties:
- They can not prevent the JVM from exiting when all the user threads finish their execution.
- JVM terminates itself when all user threads finish their execution
- If JVM finds running daemon thread, it terminates the thread and after that shutdown itself. JVM does not care whether Daemon thread is running or not.
- It is an utmost low priority thread.
Methods:
- void setDaemon(boolean status): This method is used to mark the current thread as daemon thread or user thread. For example if I have a user thread tU then tU.setDaemon(true) would make it Daemon thread. On the other hand if I have a Daemon thread tD then by calling tD.setDaemon(false) would make it user thread.
Syntax:public final void setDaemon(boolean on) parameters: on : if true, marks this thread as a daemon thread. exceptions: IllegalThreadStateException: if only this thread is active. SecurityException: if the current thread cannot modify this thread.
- boolean isDaemon():
This method is used to check that current is daemon. It returns true if the thread is Daemon else it returns false.
Syntax:public final boolean isDaemon() returns: This method returns true if this thread is a daemon thread; false otherwise
// Java program to demonstrate the usage of// setDaemon() and isDaemon() method.publicclassDaemonThreadextendsThread{publicDaemonThread(String name){super(name);}publicvoidrun(){// Checking whether the thread is Daemon or notif(Thread.currentThread().isDaemon()){System.out.println(getName() +" is Daemon thread");}else{System.out.println(getName() +" is User thread");}}publicstaticvoidmain(String[] args){DaemonThread t1 =newDaemonThread("t1");DaemonThread t2 =newDaemonThread("t2");DaemonThread t3 =newDaemonThread("t3");// Setting user thread t1 to Daemont1.setDaemon(true);// starting first 2 threadst1.start();t2.start();// Setting user thread t3 to Daemont3.setDaemon(true);t3.start();}}Output:
t1 is Daemon thread t3 is Daemon thread t2 is User thread
Exceptions in Daemon thread
If you call the setDaemon() method after starting the thread, it would throw IllegalThreadStateException.
// Java program to demonstrate the usage of // exception in Daemon() Threadpublic class DaemonThread extends Thread{ public void run() { System.out.println("Thread name: " + Thread.currentThread().getName()); System.out.println("Check if its DaemonThread: " + Thread.currentThread().isDaemon()); } public static void main(String[] args) { DaemonThread t1 = new DaemonThread(); DaemonThread t2 = new DaemonThread(); t1.start(); // Exception as the thread is already started t1.setDaemon(true); t2.start(); }} |
Runtime exception:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1352)
at DaemonThread.main(DaemonThread.java:19)
Output:
Thread name: Thread-0 Check if its DaemonThread: false
This clearly shows that we cannot call the setDaemon() method after starting the thread.
Daemon vs User Threads
- Priority: When the only remaining threads in a process are daemon threads, the interpreter exits. This makes sense because when only daemon threads remain, there is no other thread for which a daemon thread can provide a service.
- Usage: Daemon thread is to provide services to user thread for background supporting task.
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


