Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Daemon thread- Use case

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)

like image 334
LeandreM Avatar asked Sep 21 '13 13:09

LeandreM


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.
like image 122
Sergey Kalinichenko Avatar answered Sep 26 '22 12:09

Sergey Kalinichenko


Sounds like an assignment question ha ha.

You can also use them for IO because IO operation block and its best to do that in a worker thread.

Also network activity if you are waiting for things to download etc. like the response to a post request.

like image 23
ddoor Avatar answered Sep 25 '22 12:09

ddoor


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.

like image 36
Josef E. Avatar answered Sep 24 '22 12:09

Josef E.