File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Difference between class level lock and object lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference between class level lock and object lock" Watch "Difference between class level lock and object lock" New topic
Author

Difference between class level lock and object lock

Dixit Saurabh
Greenhorn

Joined: Jun 26, 2010
Posts: 5
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.



Gladwin Burboz
Greenhorn

Joined: Feb 26, 2008
Posts: 25
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:


<a href="http://www.sealordresortarnala.com" target="_blank" rel="nofollow">http://www.sealordresortarnala.com</a>
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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

Joined: Jun 26, 2010
Posts: 5
Thanks to everyone for resolving my doubt. Now it is clear for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between class level lock and object lock