aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized 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 "synchronized" Watch "synchronized" New topic
Author

synchronized

Fang Xu
Greenhorn

Joined: Dec 29, 2000
Posts: 1
Can any one help me with this question?
When synchronozed is used as a modifier to a static method, what would be the monitor?
Thanks
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Static synchronized block would lock the entire Class.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78

Static synchronized block would lock the entire Class.
Could you explain it more?
static members belong to the entire class. my teacher told me the mechanism about static member is different with the lock mechanism of sychronization, monitor so called. but he didnot claim the reason, so in my mind monitor maybe is not the mechanism of static. Moreover, i am sure the entire class is locked when static sychronized method are called.
i hope you can explain it more deep and detailly.
Thanks
George
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
Originally posted by Ajith Kallambella:
Static synchronized block would lock the entire Class.

i check the khalid's book, and the following is my understanding:
as i said before, i think the monitor of static synchronied method is different from the monitor of general sychronized method. the former is " a class-sepcific monitor" (quoted from the book); the later is just the object monitor. Plz attention one is CLASS monitor, anther is OBJECT one.
a thread acquires the class monitor before it can process with the excution of any static sychronized method in the class, blocking other threads wishing to excute any <red>such methods</red> in the same <red>class</red>.
moreover, the thread with static sychronized method doesnot effect the performance of the other threads acquiring the monitor on any object of the class to excute the only <red>sychronized</red> non-static methods.
the class monitor and object monitor are , after all, independent
each other.
that is clear? if not, plz refer to khalid's book. teh explaination is cool and like oracle(not database,just english word)!
regds
George
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3324
    
    8
Of course they are different. But this difference is only because they are different objects! As you are aware every object is a resource which acts as a lock having a key. Obviously, every object of a class is an independent lock.
In case of non-static methods the object is the same object on which the method is called. In the case static methods, the object is the class object. For every java class that is loaded by the JVM, an object of class 'java.lang.Class' is created. And this is the object which acts as the lock for static methods of that class.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: synchronized