What is a daemon thread? When should I use setDaemon() and why?

Rob Edmondson

Use thread.setDaemon(true) to tell the JVM to make the thread a daemon thread.

According to Webster's, a daemon (variant of demon) is an attendant power or spirit. Daemon threads are typically used to perform services for your application/applet (such as loading the "fiddley bits"). The core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution. Use daemons as the minions they are.

[In short: daemon threads do not keep the program from quitting; user threads keep the program from quitting. -Alex]

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.