This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Doubt in the Program on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Doubt in the Program on Threads" Watch "Doubt in the Program on Threads" New topic
Author

Doubt in the Program on Threads

Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: May 18, 2012
Posts: 29
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

Joined: Jan 28, 2003
Posts: 3946
    
  17

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.

Steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in the Program on Threads
 
Similar Threads
wait() in synchronized code ??
Avoiding deadlock situations in java
Thread synchronization
synchronized threads
please explain me why this program is not going into deadlock scenario