GeeCON Prague 2014*
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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "locks " Watch "locks " New topic
Author

locks

manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
All Objects and classes have locks.
Is this true..
I know, Objects can have locks and how abt classes??
I remember, this has been discussed here , sometime back, but, I could not follow it that time.
can anyone throw some light on this??


MT
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
All objects have locks.
Here you go for a nice ride into the JLS
JLS 17.13 Locks and Synchronization
Let's take an example:
We have the following declaration somewhere:
SomeClass someObject = new SomeClass();
For instance, when you want to lock on an object, you just have to write the following code:

Classes also have a lock. In order to lock on a class, you write the following code:

Note that SomeClass.class resolves to the Class object representing the class of someObject and not the file in which someObject is declared. Class objects are unique within all JVMs, that is there is only one instance of each Class loaded in any JVM.
[ August 16, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
"NO JLS.. Only VAL"..
Thanks Val... Thats a nice explanation again!!
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
But Val, where exactly , we can use
synchronized(SomeClass.class).. Can I get a example from you!!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
synchronizing on the class has the same effect has declaring a static method synchronized, that is only one thread will ever be able to access any object of the same class at the same time, which is not always the case with object locks if there are different objects and threads are locking on different objects.
I will give you an example later. If somebody else has one ready, just post it and we will discuss it. In the meantime, Murthy, try to come up with one example of your own and we will go into it. Practice is your best teacher
 
GeeCON Prague 2014
 
subject: locks