Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it bad to call Join on a daemon thread

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 Answers

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.

like image 87
bowmore Avatar answered Oct 28 '25 11:10

bowmore