• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic