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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With