It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object's lock 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 "Object Watch "Object New topic

Object's lock

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
When a lock is obtained on an object instance no synchronized method can be executed by anyone but the owner of the lock. Is it true that when a lock is obtained the synchronized blocks of code will also be available to the owner only.
[ May 18, 2003: Message edited by: Anupam Sinha ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I think it's the other way round: A thread becomes the owner of the object's monitor by executing that object's synchronized code.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Rattan Mann
Ranch Hand

Joined: Jan 21, 2003
Posts: 44
Yes. Often the only difference between synchronized method and synchronized code block is that you don't want to lock up the whole method but only that critical part of the method which needs synchronization. This improves performance.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Thanks a lot for the reply Roger and Rattam. Rattam your answer helped me clear this doubt.
I agree. Here's the link:
subject: Object's lock
jQuery in Action, 3rd edition