Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static and non Static Synchronize Methods

 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers

Is there any behavioral difference in synchronize static method and synchronize non static method, in a multi threaded environment.

Please explain.

Thanks
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is yes .

when a synchronize static method is called a lock is obtained on the Class (since static method can be called without creating the clas object) but when a synchronize method is called a lock is obtained on the object which has called the method.
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any change in synchronization mechanism both cases(static / non static method) ???
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[message edit: There is no change in synchronization mechanism both cases(static / non static method)]


This does not show the problem well, but when you start doing things like this it is not difficult to have variables such as name in Object one change in ways that are not expected. Not really any change in syntax or meaning of synchronized in either static or instance method.
[ April 16, 2008: Message edited by: Nicholas Jordan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic