aspose file tools*
The moose likes Java in General and the fly likes What is object locking and class locking? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is object locking and class locking?" Watch "What is object locking and class locking?" New topic
Author

What is object locking and class locking?

Nilesh Chawda
Greenhorn

Joined: Mar 07, 2013
Posts: 8
What does it mean by object locking and class locking?

Does object locking means lock on instance variables and instance methods? So that no other thread can access instance variables and instance methods.

Similarly in class locking:
Does class locking means lock on static variables and static methods? So that no other thread can access static variables and static methods.

Please help me to understand object and class lock concepts.

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nilesh Chawda wrote:What does it mean by object locking and class locking?


There's no such thing as either one. We don't lock objects or classes. We obtain an object's lock (or monitor) by synchronizing on that object. That's the only kind of locking there is.

Does object locking means lock on instance variables and instance methods? So that no other thread can access instance variables and instance methods.


No. Synchronizing (obtaining a lock) doesn't prevent access to methods or variables. It just means no other thread can obtain that lock until the thread that currently holds it releases it.

Nilesh Chawda
Greenhorn

Joined: Mar 07, 2013
Posts: 8
Then obtaining lock, what does it mean and how it achieve synchronization?
As lock doesn't prevent access to methods or variables to other threads.
drac yang
Ranch Hand

Joined: Apr 19, 2013
Posts: 61
Nilesh Chawda wrote:Then obtaining lock, what does it mean and how it achieve synchronization?
As lock doesn't prevent access to methods or variables to other threads.


java mechanism ensures that at a time only one thread could get the lock of the object, so if one thread has already got the lock, other threads accessing the critical area have to wait on that object for the lock untill the owning thread releases the lock and one of those waiting threads gets the lock.
effectively the synchronization has been performed.


science belief, great bioscience!
Nilesh Chawda
Greenhorn

Joined: Mar 07, 2013
Posts: 8
Ok Thanks.

But I think I have read some where that for same object if one thread is excecuting a synchronized method,
then for same object, other thread have to wait to get lock in order to execute other synchronized method.

That's why I am in doubt that other thread has been prevented to access other synchronized method for same object.

So, I thought getting lock means preventing access to variables and methods.

Please correct me in this context.

If possible please suggest a concept to remember for object lock and class lock, so that I can understand their function.

Thanks.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8222
    
  23

Jeff Verdegan wrote:That's the only kind of locking there is...

<nitpick>
Not quite true; at least not since the introduction of ReentrantLock's.
</nitpick>

Nilesh Chawda wrote:Then obtaining lock, what does it mean and how it achieve synchronization?
As lock doesn't prevent access to methods or variables to other threads.

Nilesh,

Drac has explained the basic mechanism, but there's a lot to know about this stuff - more than can be covered in a forum like this - so I suggest you read the tutorials. Even then, they will cover the mechanics, not necessarily best practises.

The best advice I can give you is that multi-threaded programming is not simple - indeed, Java may be guilty of making it seem too easy. This is not a reason not to learn it, nor to use it but, as with reflection, you should be sure that it actually provides benefit before undertaking it.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nilesh Chawda
Greenhorn

Joined: Mar 07, 2013
Posts: 8
Ok Thank you.

I will go through tutorials.

But can you please help me with below sentence whether it is correct or not.
If not please correct me :

I have read some where that for an object if thread1 is executing its synchronized instance method,
then for same object, thread2 have to wait to get lock in order to execute other synchronized instance method while
thread1 executing a synchronized instance method.

And similarly for static methods of a class.



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Jeff Verdegan wrote:That's the only kind of locking there is...

<nitpick>
Not quite true; at least not since the introduction of ReentrantLock's.
</nitpick>



Fair point.

I should have said there's only one kind of locking achieved with the synchronized keyword, and there are the higher level lock objects in java.util.concurrent, but none of that is class vs. object.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nilesh Chawda wrote:
I have read some where that for an object if thread1 is executing its synchronized instance method,
then for same object, thread2 have to wait to get lock in order to execute other synchronized instance method while
thread1 executing a synchronized instance method.

And similarly for static methods of a class.


That is correct. And both those cases are identical, because in both cases, T1 has obtained the lock for an object and therefore T2 cannot enter a region protected by that same lock until T1 releases it.

It's worth noting that declaring a method synchronized is just syntactic sugar for locking that method's body on a particular object, and there's no "class lock" vs. "object lock" involved.



In all four methods above, we simply obtain an object's lock, which prevents other threads from obtaining that same lock until we release it.
 
 
subject: What is object locking and class locking?