File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes object lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "object lock" Watch "object lock" New topic
Author

object lock

cybel sheriden
Ranch Hand

Joined: Jan 18, 2005
Posts: 50
hi
iam i correct in stating these 3 statement.

1> when u synchronize a method, lock of object used to invoke this method should be acquired.


2>
method()
{ synchronize(this) { }
}
above code lock is obtained on object used to invoke the method



3>
method()
{
synchronize(a){}
}
in the above code lock is obtained on object refered by 'a' and of the object used to invoke the method, so 2 locks obtained
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Threads and Synchronization...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
1 yes
2 yes
3 no, only the lock on a is held


Mike Gershman
SCJP 1.4, SCWCD in process
cybel sheriden
Ranch Hand

Joined: Jan 18, 2005
Posts: 50
if in the third case the lock is obtained on only object ref by 'a',then as per k&b page 523 "third party object lock for synchonizing a piece of code gives ability to have more then one lock for code synchronisation within a single object".
so i assumed tht 2 locks are obtained for the 3rd code sample.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Hi

Cybel, I think you talk about this:


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by George Bolyuba:
Hi

Cybel, I think you talk about this:


In this case, there is only one lock obtained. Since the executing thread already has a lock on the object "a", the second synchronized block really does nothing for you.


James Carman, President<br />Carman Consulting, Inc.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
No lock is obtained at all as the code won't even compile...


42
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: object lock