• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
amol l a lekurwale
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amol l a lekurwale
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20163
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic