Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads, wait, notify and deadlocks.

 
Chris Johansen
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm currently going through some mock exams from the CD that comes with the book, and I came across a problem I'm not sure about. The question asks whether the following statement is true or false:

"Deadlock will not occur if wait()/notify() is used"

I thought wait() releases its locks, and therefore it would not cause a deadlock? I guess I'm wrong as I haven't seen this in any errata or such, but I still can't figure out why. :P
 
Harsha Smith
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() and notify() and notifyAll() are not part of the exam anymore.
 
Stephan van Hulst
Bartender
Pie
Posts: 4840
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() may still cause deadlock, as demonstrated by this simple program:
 
Chris Johansen
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:wait() may still cause deadlock, as demonstrated by this simple program:


As I'm not very familiar with Threads/synchronized methods/++ you'll have to excuse my potential misunderstanding, but your example seems rather like an object waiting for "access" (not quite the right word to use) than deadlocking? Deadlocking can be defined to when two threads are waiting for each other's locks to be released; therefore, the locks will never be released (from Sierra / Bates book). In the example it's not two objects, nor is it two Threads.
 
Chris Johansen
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:wait() and notify() and notifyAll() are not part of the exam anymore.


Thanks. I still want to figure this one out though. Where did you find that these subjects no longer are a part of the exam? I failed to find it.. :-/
 
Stephan van Hulst
Bartender
Pie
Posts: 4840
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I guess if you define a deadlock as two threads waiting for ownership of two object monitors, both held by the other thread, then sure; wait/notify won't cause deadlock by themselves.

However, the object monitor is implemented the same way the wait() method is, so I don't really know if you should care about calling it "deadlock" or just a program that hangs.
 
Harsha Smith
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Topics that have been removed
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic