• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

lock for instance method and class static method

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know, we can use synchronized to instance method and class static method. Synchronized means one thread has to hold lock (monitor). Then in the both synchronized method (static and instance), the lock (monitor) is same ?

thanks.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Chen wrote:As we know, we can use synchronized to instance method and class static method. Synchronized means one thread has to hold lock (monitor). Then in the both synchronized method (static and instance), the lock (monitor) is same ?


No. There are two locks namely "class lock" & "instance lock". "class lock" is associated with the Class object of that class and the instance lock is associated with the object itself. So you get instance lock for each instance you create. Static blocks synchronize on "class lock" while instance lock synchronizes on instance (object) lock.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in above code synchronized block was done in Class Level and the entire class was LOCKED.




In the above code only the object was get locked not the class....


 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammed Yousuff wrote:
in above code synchronized block was done in Class Level and the entire class was LOCKED.


I don't know what you mean by "entire class was LOCKED.. " but you still able to invoke any other instance methods (if any) of a given instance of that class.And the reverse is also true for instance locks, that is you are still able to invoke static/non-synchronized instance methods in case of instance lock.
 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijitha Kumara ...You are right...

"entire class was LOCKED.. " I just mean that all static method in the Class will get LOCKED...

i think we both are Sailing in the same boat

 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic