aspose file tools*
The moose likes Threads and Synchronization and the fly likes synchronized(WhatIsThis) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized(WhatIsThis)" Watch "synchronized(WhatIsThis)" New topic
Author

synchronized(WhatIsThis)

Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
Obtains lock of 'this' object, which is clear. But, if the thread obtains the lock of 'obj', how lock of object on which this method is called is obtained?
In other words, what is the difference?
Please explain.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Each Java object has a lock associated with it.

The difference is that in the first example, the lock of the object that the method is called on is used (because that's the object that this refers to), and in the second example, the lock of the object that the variable obj refers to is used.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
then, if I'm calling a method inside synchronized block, while the thread is inside that method, other thread also can call same method on same instance of class A as lock of that instance is not obtained in case2.
Correct me if I'm wrong.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Jayavardhan geepi wrote:In other words, what is the difference?
Please explain.

I think Jesper covered most of it; however, a concrete example might be a case where you want to keep "reads" and "writes" separate. If you just synchronize on 'this', the two types of method would be competing for the same lock when they don't need to, so an alternative might be:Do you see what's happened? The code is still synchronized, so only one read or write can occur at a time, but the methods don't interfere with each other so a read can take place while a write is happening (not sure you'd actually want to do that, but it's just for illustration).

You may also want to look at ReentrantLock: a very useful new addition to the concurrent package.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
Am I right here?..
I have, and I'm doing from inside 1st synchronized block of above example by Winston.
So, only someRead method is synchronized irrespective of on which instance it is called.
i.e even subsequent code has to wait until first call to someRead() is finished?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Jayavardhan geepi wrote:read1.someRead();[/code] from inside 1st synchronized block of above example by Winston.

No, synchronization works from outside, and you don't have any in the example you showed.

Basically, a synchronized block can only be run by one Thread at a time; however, what you synchronize on determines whether (or when) the Thread is blocked or not. A synchronized method always synchronizes on this, so if you don't want to block all other synchronized methods for the object, you need to find some other way of doing it. The example I gave you above is just one (and it is just an example; I wouldn't expect anyone to code it that way).

Winston
Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
Clear.
Thanks Winston and Jesper.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And make sure you understand that syncing only stops other threads from entering other synced blocks or methods on the same lock. It has no effect on another thread calling an unsynchronized method or executing code that's not in a synchronized block.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized(WhatIsThis)