This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Difference between class level lock and object lock

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one doubt regarding class level lock and object level lock. It is clear for me if we want to synchronize our static methods then we make these method synchronized and take a class level lock for this.

But my doubt is: If we have a static method that accesses a non-static field (using an instance) and same non static field accessed by other non static synchronized method. Then How we can manage synchronization for this instance variable because two different thread can access synchronized static method and synchronized method.



 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two different threads cannot access synchronized static method of a same class as one thread will first get the monitor of class object instance.

Also if there are two threads as below
T1. Thread that has acquired lock on an object instance say O1
T2. Thread from synchronized static method which will attempt to access non-static field using object instance O1

T2 has acquired lock on class-object instance and not on object instance O1.
When T2 attempts to access O1, it will see that O1 is already locked by T1 and will get blocked.

And T2 should acquire lock on O1 as follows:
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, a thread cannot enter a synchronized block of code until it holds
the appropriate object lock. For instance methods, this is the lock of an instance.
For static methods this is the Class object lock.

Jim ... ...
 
author
Posts: 23924
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dixit Saurabh wrote:
But my doubt is: If we have a static method that accesses a non-static field (using an instance) and same non static field accessed by other non static synchronized method. Then How we can manage synchronization for this instance variable because two different thread can access synchronized static method and synchronized method.



Another way to think of it is this... The JVM will enforce the locking mechanism. To make sure that only one thread can own the lock at one time. It is *not* a real lock in this regard. There is no locking of accesses, etc. Once you own the lock, you do the access. And if you code the program, such that there is a way to get to a variable by not owning the lock, or by owning different locks (in this example), then is it a flaw in your design.

Henry
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good points about owning locks. Sensitive data should be handled in properly synchronized code.
I say "handled" because this does not apply only to data changes. For example, one thread reports
bank account balances while another thread does account transfers. Say the transfer thread properly
takes locks on the 'from' and 'to' account objects. But if the reporting thread does not, it can show a
decreased balance in the 'from' account and the 'to' account balance before it has been increased.

As for class level state, it may be safest to make changes only through synchronized class methods.

Jim ... ...
 
Dixit Saurabh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everyone for resolving my doubt. Now it is clear for me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic