• 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

Deadlock

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 : )
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic