Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronize instance method call in a static method  RSS feed

 
Rajitha Gunawardhane
Ranch Hand
Posts: 43
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to know if we declare an instance object in a static method and call a synchronize method of that object; when multiple threads accessing tat static method will that synchronization works fine? or not.

Hope You can understand above explanation.

Thanks,
Rajitha.
http://dvrajitha.blogspot.com
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you right each static method call will create a new (its own copy of the instance object ie its not static just local) so the result will be no synchronization as each call is creation of a new sync object, hence new lock each time (which interestingly can be optimised away). Want to post a code snippet that would make it obvious ? Sounds like you want to use a static field on the class or the class definition instance itself to sync on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!