• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about class lock.  RSS feed

 
Jakhongir Raimbekov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, dear friends. Please, help to understand class and object locks. I think like this: if any thread keeps the lock of the object, other threads cannot access instance methods. If class lock is keeped, then class (static) methods are not available to other threads, untill the lock is released. Please, correct me if I am wrong.

Consider the following code :

The output is :
class MyThread is locked!
Executing static method of class MyThread
class MyThread is released!

But why? Cannot understand the logic.
Thanks for your answers in advance.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jakhongir Raimbekov wrote:Hi, dear friends. Please, help to understand class and object locks. I think like this: if any thread keeps the lock of the object, other threads cannot access instance methods. If class lock is keeped, then class (static) methods are not available to other threads, untill the lock is released.


In Java, synchronization locks are cooperative; so, it may be better to say... "if any thread acquires the lock of the object, other threads cannot acquire the same lock of the object".

The idea of how object locks and class locks are related to instance methods and class methods is merely an implementation detail of how the synchronized keyword is applied to methods. And even then, it is cooperative -- methods that aren't synchronized can always run without delay (as they won't be acquiring any locks).

Henry
 
Jakhongir Raimbekov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Jakhongir Raimbekov wrote:Hi, dear friends. Please, help to understand class and object locks. I think like this: if any thread keeps the lock of the object, other threads cannot access instance methods. If class lock is keeped, then class (static) methods are not available to other threads, untill the lock is released.


In Java, synchronization locks are cooperative; so, it may be better to say... "if any thread acquires the lock of the object, other threads cannot acquire the same lock of the object".

The idea of how object locks and class locks are related to instance methods and class methods is merely an implementation detail of how the synchronized keyword is applied to methods. And even then, it is cooperative -- methods that aren't synchronized can always run without delay (as they won't be acquiring any locks).

Henry


What do you mean by cooperative, Henry? Do you mean that race competition is about acquiring the lock, not about the object or class, i.e. if one thread acquired the lock, the other one can't do it, but it can use any non-synchronized methods or synchronozed on other lock?
 
Jakhongir Raimbekov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you a lot Henry. Now it seems to me clear.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jakhongir Raimbekov wrote:i.e. if one thread acquired the lock, the other one can't do it, but it can use any non-synchronized methods or synchronozed on other lock?


yes
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!