wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Difference between synchronized(this) and synchronized(other external object) 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 "Difference between synchronized(this) and synchronized(other external object)" Watch "Difference between synchronized(this) and synchronized(other external object)" New topic
Author

Difference between synchronized(this) and synchronized(other external object)

amol l a lekurwale
Ranch Hand

Joined: Oct 16, 2008
Posts: 30
I am using a method from a class, which has been written by someone else…

Part of the method is synchronized. However, the part within the synchronized portion does the actual job for which the method has been called.

The block has been synchronized on ‘this’. However, as many times I call the method, the executing thread fails to acquire lock for the block.



Question I : If I synchronize on ‘this’, does this mean that any other method in that class, which changes the instance variables, called from somewhere else, will fail or succeed?

Question II : If I synchronize on anything else, does that mean I will get the same result in all conditions as it was by synchronized on ‘this’? Actually, what difference it makes if synchronized on ‘this’ else synchronized on any third party object?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

Question I : If I synchronize on ‘this’, does this mean that any other method in that class, which changes the instance variables, called from somewhere else, will fail or succeed?


If you synchronize on 'this', it means that any other thread that synchronizes on the same object will have to wait until you release the lock before it can proceed. It also means that you will have to wait, if some other thread owns the lock, for the lock to be released before you can proceed.


Question II : If I synchronize on anything else, does that mean I will get the same result in all conditions as it was by synchronized on ‘this’? Actually, what difference it makes if synchronized on ‘this’ else synchronized on any third party object?


If you synchronize on "anything else", it means that any other thread that synchronizes on the same "anything else" will have to wait until you release the lock before it can proceed. It also means that you will have to wait, if some other thread owns that lock, for the lock to be released before you can proceed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amol l a lekurwale
Ranch Hand

Joined: Oct 16, 2008
Posts: 30
Thanks Henry for your response.
But the main query was...:
I have two methods in my class, which change the same instance variable.
One has a block which synchronizes on 'this' and other has a block, which synchronizes on a third party object, both changing the same instance variable.
So, now is it gauranteed that both threads, which execute the respective methods block, will wait for other to complete, or they will be as good as not synchronized?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If they synchronize on the same object, then they can lock each other out. Otherwise, not. It's pretty simple. You can make the examples as complicated as you like, but the rules are still simple.
amol l a lekurwale
Ranch Hand

Joined: Oct 16, 2008
Posts: 30
Thanks Paul.
I have one more question.
If I call a synchronized block in a non static method, from a static method(not synchronized), will the thread aquire lock on 'this' in non static method, and execute the block.
Assume that the reference of the object of the non static method containing class has already been initialized before call from the static method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

If I call a synchronized block in a [DELETE], [DELETED], will the thread aquire lock on 'this' in non static method, and execute the block.



I deleted part of your question (in this response) because it is not relevant -- it doesn't matter what the method is, and where is it called from. To answer you question...

The thread will acquire the lock on the object specified in the synchronized block, and execute the block.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between synchronized(this) and synchronized(other external object)