jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static and non Static Synchronize Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static and non Static Synchronize Methods" Watch "Static and non Static Synchronize Methods" New topic
Author

Static and non Static Synchronize Methods

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Hi ranchers

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

Please explain.

Thanks


"Know where to find the solution and how to use it - that's the secret of success."
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
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.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
is there any change in synchronization mechanism both cases(static / non static method) ???
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[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 ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
jQuery in Action, 2nd edition
 
subject: Static and non Static Synchronize Methods