• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread dies, implicit notifyAll?

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you please explain this sentence from Exam Cram 2 Java 2 Programmer, Chapter 10, page 200
"If a thread that has a lock on one or more objects dies, the JVM removes the locks and does the equivalent of notifyAll for each object locked."
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think basically it means that if a thread dies the system calls a notifyAll() for each object lock the dead thread had so that the threads waiting for the various objects that were locked can return to the runnable pool knowing the locks have been released.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a thread holding the lock of an object dies, other threads waiting on the object do not become runnable. Therefore that could not be what the sentence means.

m1: before wait
m2
true
false
[ July 08, 2003: Message edited by: Marlene Miller ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, it sounds like the book is just plain wrong here. Your example code is a good illustration. Perhaps add a Thread.sleep(100) between the two start() calls to ensure that things alway happen in the order you expect - but the output makes it fairly clear that t1 was in wait before t2 ran (at least for one run, if not always guaranteed). And the specs (e.g. JLS 17.14) are pretty clear that once a thread calls wait(), it only exits due to an actual notify(), notifyAll(), or timeout. Nothing about an "equivalent to notifyAll()" when a thread dies. But the authors are pretty smart guys, and I don't have a copy to see the full context of this comment, so perhaps there's another meaning that I'm missing.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jim for giving my question some thought and looking over my example. Thank you for your time. It helps to know that I am not overlooking something obvious.
Even if it is not literally true, I can't help but wonder what the authors were trying to say.
I have this new idea. Doesn't it sound suspiciously like a description of join()? I propose this paragraph in the section Coordinating Threads with wait and notify belongs in the next section Coordinating Threads with join.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the intent of that sentence was to cover the situation when a Thread holding locks dies abruptly due to an exception. In that case the JVM unlocks the objects as it unwinds the stack. Obviously it did not turn out right.
That sentence was inherited from the first edition and Marlene is the first person to call attention to the problem! Maybe a career as a tech editor is in order?
Bill
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bill.
If I had suspected it was an error in the text, I would not have put it on the forum. I would have sent you an errata note. I thought for sure I just wasn't reading it the way I should be. I apologize.
By the way, I am working through the questions in the text and find them to be very intelligent. You have put a lot of thought into them. Thank you.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic