wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads, wait, notify and deadlocks. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads, wait, notify and deadlocks." Watch "Threads, wait, notify and deadlocks." New topic
Author

Threads, wait, notify and deadlocks.

Chris Johansen
Greenhorn

Joined: Nov 03, 2011
Posts: 4
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

Joined: Jul 18, 2011
Posts: 287
wait() and notify() and notifyAll() are not part of the exam anymore.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3574
    
  14

wait() may still cause deadlock, as demonstrated by this simple program:
Chris Johansen
Greenhorn

Joined: Nov 03, 2011
Posts: 4
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

Joined: Nov 03, 2011
Posts: 4
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

Joined: Sep 20, 2010
Posts: 3574
    
  14

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

Joined: Jul 18, 2011
Posts: 287
Topics that have been removed
 
Don't get me started about those stupid light bulbs.
 
subject: Threads, wait, notify and deadlocks.
 
Similar Threads
How to avoid Deadlock (Wait with two locks)
Question about Deadlock
lingering wait()/notify() and deadlock doubt
Alternative to suspend()-resume()
wait/notify deadlock