Home > DeveloperSection > Forums > What is Daemon thread in java
sandy taylor
sandy taylor

Total Post:46

Posted on    September-03-2015 12:10 AM

 Java Java  Thread 

 1 Reply(s)
 838  View(s)
Rate this:
I am very much confused about daemon threads, can anyone tell me what is daemon threads and why we use them?

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    September-03-2015 6:09 AM

A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.

You can use the setDaemon() method to change the Thread daemon properties.When a new thread is created it inherits the daemon status of its parent.
Normal thread and daemon threads differ in what happens when they exit. When the JVM halts any remaining daemon threads are abandoned:

finally blocks are not executed,
stacks are not unwound - the JVM just exits.

Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.

Don't want to miss updates? Please click the below button!

Follow MindStick