Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection.
- 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.
- 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.
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.
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.
// Checking whether the thread is Daemon or not
" is Daemon thread"
" is User thread"
DaemonThread t1 =
DaemonThread t2 =
DaemonThread t3 =
// Setting user thread t1 to Daemon
// starting first 2 threads
// Setting user thread t3 to Daemon
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.
Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.setDaemon(Thread.java:1352) at DaemonThread.main(DaemonThread.java:19)
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 email@example.com. 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.