3

I was reading this SO post about daemon threads and the quote at the bottom of the answer is:

But joining a demonized thread opens most likely a whole can of trouble!

Why exactly is it considered bad practice? I understand .join() blocks regardless if the thread is daemon or not, but I don't understand why it's considered bad practice. Can someone explain?

  • 1
    From what I understand, a thread is set to be a daemon thread when it is not very important and the main program doesn't have anything to do with its result. That's why it is set as a background process and is not required to finish when the program quits. From the quote, I believe the reason for the can of worms is the design choice or the reason for making those threads as daemons. Not sure but that would be my guess. You can do it if you know what you are doing and have a reason to have made those threads as daemon. – clinomaniac Nov 22 '17 at 19:36
  • 1
    If you need to wait for the tread to come back then may be it should not be daemon. Hope this helps. – clinomaniac Nov 22 '17 at 19:37

1 Answers1

4

The relevance of a daemon thread, and its definition, is that it doesn't prevent the JVM from exiting when the program finishes but the thread is still running.

For any thread, designed to run and end before the program finishes, it is useless to be a daemon thread.

From this it is logical to conclude that any well designed daemon thread is designed to run as long as the program runs.

Joining on a daemon thread, therefore, implies the join will block until the daemon thread ends, which, assuming it is a well designed daemon thread, is never. If this blocking join() prevents further useful code to be executed, that will never happen, and possibly, your code will be stuck.

bowmore
  • 10,842
  • 1
  • 35
  • 43
  • "For any thread, designed to run and end before the program finishes, it is useless to be a daemon thread. From this it is logical to conclude that any well designed daemon thread is designed to run as long as the program runs." I don't think that follows. Say I have a thread that reads lines of input from stdin, and feeds those lines one by one to the program, through a queue. On one run, the thread might run to completion before program finishes. On another run, the program might decide to exit first. I make the thread a daemon so it won't prevent the program from exiting. Reasonable? – Don Hatch Oct 19 '18 at 07:19
  • @DonHatch Firstly (from JCIP) : *"In particular, it is dangerous to use daemon threads for tasks that might perform any sort of I/O."* This is because daemon threads can be ended without any chance of cleanup. Secondly : the example you give is one that is *designed* to run and end before the end of the program, that doesn't imply it cannot be canceled prematurely. – bowmore Oct 19 '18 at 07:52
  • The Firstly doesn't sound like a problem; clean up what? It's just reading from a file or something, and communicating the results to the main program who might, at some point, just decide it doesn't care about the rest, and exit. And the Secondly isn't right: the thread is designed to *either* run and end before the end of the program (in which case it doesn't matter whether the daemon flag is set or not) *or* to automatically die if the main program dies first (therefore the daemon flag must be set, to make that happen). I'm still not seeing any problems with this setup. – Don Hatch Oct 19 '18 at 12:39
  • BTW it's possible we're talking past each other-- I'm thinking of Python, as described [here](https://docs.python.org/3/library/threading.html#thread-objects), where the `daemon` flag means nothing more or less than "automatically dies when the main program dies". Perhaps Java puts additional meaning on the word "daemon" that would explain your concern? (The question is tagged with both Python and Java.) – Don Hatch Oct 19 '18 at 12:43
  • @DonHatch if it's reading from a File, not allowing the thread to close it's open file may leave open File handles on the OS, which is exactly why JCIP warns against using such a resource on a daemon thread. – bowmore Oct 19 '18 at 13:14
  • What OS are you talking about exactly, in which you get a file handle leak from exiting a program that's in the middle of reading a file? That's never been a possibility in any environment in which I've programmed (unix/linux). – Don Hatch Oct 19 '18 at 13:26
  • @DonHatch I must admit I've not actually tried this. What I do know is that all related Java literature warns against using a daemon thread for doing I/O operations, because resource cleanup won't happen (don't know about Python). – bowmore Oct 19 '18 at 13:42
  • @DonHatch "I make the thread a daemon so it won't prevent the program from exiting. Reasonable?" There are proper ways to cancel a thread in Java that do allow it to do proper clean up. – bowmore Oct 19 '18 at 13:49