Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is object locking and class locking?

 
Nilesh Chawda
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nilesh Chawda
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nilesh Chawda
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic