File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Deadlock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Deadlock" Watch "Deadlock" New topic
Author

Deadlock

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
An example from JLS


This example would deadlock if a single thread were not permitted to lock a lock more than once.

HOw could the deadlock happen? Can a thread lock an object it already hold a lock on?


SCJP 1.4
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy!
If a thread gets a lock, and while holding that lock enters code synchronized on that same object, the JVM can see that, "Oh, this guy already HAS that lock, so no problem". This lets you have re-entering code for example where thread A is cruising along, and grabs a lock, then invokes a method on some object who in turn invokes a method back on the original object. So within the same thread of execution, a single thread encounters code TWICE (or more) that is synchronized on the object he (the thread) already has the lock for.
You can think of it as though the JVM just knows you already have the lock, so it doesn't need to worry about the fact that you're entering protected code since -- you already picked up the lock elsewhere.
Or... you could think of it as a special privilige that threads have, where they can lock an object more than once, but only if there initially had the lock in the first place.
I have no idea if any of this made sense, since I'm about to fall asleep, but I hope it helps.
Cheers,
Kathy : )


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46


HOw could the deadlock happen? Can a thread lock an object it already hold a lock on?

Here we go...
--Answer for the first part of your question:
Referring to your code snippet (I added a print statment that says "At Level 1" ) if the JVM doesn't allow the Thread that enters first synchronized statment to lock the same object again before entering the second synchronized block then the deadlock will happen. Think again, if it don't allow the same thread to lock the same object again then it would stay at Level 1 i.e between the two synchronized blocks waiting for the lock to be released to enter the second synchronized block, but the same thread which is waiting to obtain the lock as the lock !!!, then this would end up in dead lock.
-- Answer for the second part:
Yes any thread that has to enter a synchronized block of code has to get a lock on the specified object, hence it is allowed for a Thread to get lock on an object multiple times.
Hope this helps
Mallik HM
[ November 08, 2002: Message edited by: Mallik HM ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deadlock