7

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
  • 1
    @user976025 your post should be marked as community wiki, as it is prone to discussion instead of requiring a precise and definite answer. Commented Sep 21, 2013 at 13:34

3 Answers 3

5

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.
Sign up to request clarification or add additional context in comments.

4 Comments

I am thinking about the suggestion of sending user statistics(about performance of a tool or activities of a user on that tool) like those of eclipse or any other tool, i mean generating those kind of reports in a daemon thread isn't it right?
@user976095 Absolutely, these sorts of network I/O tasks should be done on daemon threads. To me, the general daemon/non-daemon "cutoff" goes along the "for the user" vs. "for the program" line. Sending the stats is definitely "for the program", not "for the user". Users would not appreciate waiting for your stats thread to finish when they close the program, so the thread should be running in the daemon mode.
upvoted, are you saying all network I/O should happen in daemon threads. I have a websocket publisher in js that continuously sends numbers and a redis subscriber in python that needs to listen to these messages to process them, i am stuck between using a normal thread and a daemon thread for this, i am doing data analytics on the incoming numbers, what do you think is the right thread mode
@PirateApp I did not mean doing all network I/O on daemon threads, only listening for incoming connections. Once the connection is made, handing the traffic with the connected peer could be offloaded to a worker thread.
2

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.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.