This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Locks " Watch "Locks " New topic
Author

Locks

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180

when a thread enters a synchronized block it will have lock on the object specified. Will it have lock on the this object also??? Means the object used to invoke the method in which the synchronized block is.
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

No, it will acquire only the lock of the mutex obect specified inside the synchronized block.


SCJP 5.0 and now fighting with ExtJS
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

Yes, it's true that you will synchronize on the intrinsic lock of the String object associated with the literal.
However, this is a terrible idea, because the String object will be part of the String constant pool.
This means any class that uses the same literal as a synchronization option, will be contending for the same lock.
If you absolutely must use a String object for this purpose, make sure you initialize the String object with the new keyword, instead of directly using a literal, or a String reference initialized with a literal value.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Locks