• 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

Object Lock and a class level lock

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,

can anyone explain what a class level lock is and what an object lock is ?
I know it is a mechanism but how is the lock associated with an object . Is it a byte code associated with an object instance .
Also , can we acquire a class level lock in another class ie if we have two classes , class ABC and class DEF .can class DEF acquire the class level lock for ABC , and if it can please explain with code
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ragi singh wrote:
can anyone explain what a class level lock is and what an object lock is ?



ragi singh wrote:
can we acquire a class level lock in another class ie if we have two classes , class ABC and class DEF .can class DEF acquire the class level lock for ABC , and if it can please explain with code


Possible with help of synchronized block[also known as critical section]



hope this helps
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seetharaman, synchronized keyword is missing on both the methods in the first example.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Good catch Sunny , also both these Class level lock and object level lock are different , so never share a shared resource (which you are trying to protect ) in two separate synchronized method where one is static and other is non static because they lock on different object and can be executed simultaneously by two different thread.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunny Bhandari wrote:Seetharaman, synchronized keyword is missing on both the methods in the first example.


Thanks. Good Catch!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A related discussion: https://coderanch.com/t/233142/threads/java/synchronized-keywords-static-methods may help
 
reply
    Bookmark Topic Watch Topic
  • New Topic