Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized

 
Fang Xu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static synchronized block would lock the entire Class.
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic