Link Details

Link 80081 thumbnail
User 214988 avatar

By puredanger
via binkley.blogspot.com
Published: May 09 2008 / 09:30

I just read Thread Signaling from Jacob Jenkov. But it has one fatal flaw: it uses a literal java.lang.String for coordinating between threads. Why is this wrong?
  • 18
  • 0
  • 1526
  • 430

Comments

Add your comment
User 131196 avatar

Jakob Jenkov replied ago:

0 votes Vote down Vote up Reply

yes, indeed a "fatal" flaw... not :-) The only real problem is that you risk waking a few too many threads, that weren't supposed to
wake up. And what exactly does that equal? Yes, that's right, a Spurious Wakeup! ... That is why we wrap the wait() call in a while loop.
So, though I agree it is bad practice to use String's as monitor objects, it is by no chance "fatal". Nothing will happen except
you risk waking up a few threads that will then go back to waiting immediately.

I fixed the text now though, it will be online on monday or so.

Later texts in that trail will show more useful constructs like locks, read/write locks, reentrant locks, resource guards,
semaphores, blocking queues etc. I will be more careful in these texts with the choice of monitor object ;-)
,

User 131196 avatar

Jakob Jenkov replied ago:

0 votes Vote down Vote up Reply

Hmm... I dug a little bit more into this topic, and I believe that in some situations using an interned string as monitor object (calling wait() / notify() on it), can actually lead to missed signals. I'll update the text explaining this. So, if this is true, it is indeed a fatal flaw!

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.