Java has a special kind of thread called daemon thread.
- Very low priority.
- Only executes when no other thread of the same program is running.
- JVM ends the program finishing these threads, when daemon threads arethe only threads running in a program.
What are daemon threads used for?
Normally used as service providers for normal threads.Usually have an infinite loop that waits for the service request or performs the tasks of the thread.They can’t do important jobs. (Because we don't know when they are going to have CPU time and they can finish any time if there aren't any other threads running. )
A typical example of these kind of threads is the Java garbage collector.
There's more...
- You only call the
setDaemon()
method before you call thestart()
method. Once the thread is running, you can’t modify its daemon status. - Use
isDaemon()
method to check if a thread is a daemon thread or a user thread.