Daemon Threads provide services for user threads, apart from gc What is another example (case) where a daemon thread can be used? (Any task(logic) that can be inside the run() method of a daemon Thread in practice)
- 1@user976025 your post should be marked as community wiki, as it is prone to discussion instead of requiring a precise and definite answer.sc4r137– sc4r1372013-09-21 13:34:30 +00:00Commented Sep 21, 2013 at 13:34
3 Answers
Here is a short list of when you may want to use a daemon thread:
- Collecting statistics and performing the status monitoring tasks - Sending and receiving network heartbeats, supplying the services to monitoring tools, and so on.
- Performing asynchronous I/O tasks - You can create a queue of I/O requests, and set up a group of daemon threads servicing these requests asynchronously.
- Listening for incoming connections - daemon threads are very convenient in situations like this, because they let you program a simple "forever" loop, rather than creating a setup that pays attention to exit requests from the main thread.
4 Comments
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.
In short: daemon threads do not keep the program from quitting; user threads keep the program from quitting.