• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reentrant Locks and Synchronization

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I have the following snippet:



This snippet is from the Java Concurrency in Practice book. I understand what a Reentract lock is. What I do not understand is that the thread that is calling the doSomething method on the LoggingWdget would actually try to acquire a lock on the LoggingWidget class. Isn't it? The description in the book says that the thread executing doSomething tries to acquire a lock on Widget? How is that? May be I lack some understanding. Couly anyone please clarify?
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In this example, the "this" object is used as the lock. It is grabbed twice; once when the doSomething() method is called. And again, when that method calls the super.doSomething().

As for the type, the "this" object should be IS-A LoggingWidget, and IS-A Widget here.

Henry
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you mean to say when i called doSomething() then "this" was LoggingWidget. So it acquires lock on it and
when super.doSomething() is called then "this" was Widget. Hence it acquires lock on it.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:So you mean to say when i called doSomething() then "this" was LoggingWidget. So it acquires lock on it and
when super.doSomething() is called then "this" was Widget. Hence it acquires lock on it.


The type of the "this" reference within an instance method of the LoggingWidget class is of type LoggingWidget. The type of the "this" reference within an instance method of the Widget class is of type Widget. The actual type of the instance is whatever it is, and it is the same instance for the example call.

I don't know what you mean by "hence it acquires lock on it". It acquires the lock because the method is synchronized. There is no cause and effect between the type of the reference and acquiring the lock.

Henry
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.. So my question is when it is calling super method then which lock it will carry. LockingWidget one or Widget.
As both methods are synchronized.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Thanks Henry.. So my question is when it is calling super method then which lock it will carry. LockingWidget one or Widget.
As both methods are synchronized.


In this example, it is the *same* object. And the object lock is being acquired twice. Why does it matter what is the type of the reference that points to it? The object is used as the lock, and not the reference variable.

Henry
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:So you mean to say when i called doSomething() then "this" was LoggingWidget. So it acquires lock on it and
when super.doSomething() is called then "this" was Widget. Hence it acquires lock on it.


Here the 'this' is a single object throughout the code snippet. But this 'this' object is of type 'LoggingWidget' as well as 'Widget' at the same time. So, once 'this' lock got acquired , one doesn't need to acquire the same lock again separately to enter the synchronized function it can call, be it in the same class or be it in the super class.
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh thanks Henry. I was having an impression that it will use 2 different objects locks(Child and parent class).
It will have lock on 1 object only.
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanks Amar.. I was in wrong impression.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!