This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in the Program on Threads

 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package may25th;

If we lock a subclass object do we implicitly lock super class?
For that question i have written the following program. Please tell me whether I am right or Wrong



[Fixed code tags]
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You lock an object, not a class. If you obtain a lock on a Lock2 object, then you've obtained a lock on that object regardless as to whether you reference it as a Lock2, Lock1 or Object variable.

Or to put it another way - each object only has one lock, regardless of how far down the inheritance hierarchy it is.

Does that make sense to you?
 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it means that when we acquire a lock on sub class object we implicitly lock the super class. Is this right? correct me if I am wrong
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anudeep Duvvuri wrote:So it means that when we acquire a lock on sub class object we implicitly lock the super class. Is this right? correct me if I am wrong


No, he is saying that you lock the Object, not the class so super class and sub class does not come into play. So in your example you actually have 2 objects. You have an instance of Lock2 which has a synchronized lock1() method. You also have a different Object which is an instance of Lock1 which has an unsymchronized lock1() method. By making Lock2.lock1() synchronized you do not affect the Lock1.lock1() method at all. In addition, if you made Lock1.lock1() synchronized, then the two methods would not be mutually exclusive be you have two different Objects and therefor different locks.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anudeep Duvvuri wrote:So it means that when we acquire a lock on sub class object we implicitly lock the super class. Is this right? correct me if I am wrong


The question has no meaning. There is no such thing as "locking a class". There's not even such a thing as "locking an object."

There is exactly one object. It IS-A superclass and it IS-A subclass. That one object has one lock. When we synchronize on that object, we obtain its lock.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm saying that refering to locking a "class" is misleading. You lock an object, and that affects all code that is synchronized on that object. That may include methods that are defined in a superclass, if they synchronize on this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:You lock an object


Even that terminology is inaccurate, or at least misleading, IMHO. It makes it sound like you're denying all access to that object, when that's not what's happening. I think it's more accurate to say we lock a block or method on an object, or just that we obtain an object's lock.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic