aspose file tools
The moose likes Threads and Synchronization and the fly likes Static synchronization vs instance var synchrnisation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Static synchronization vs instance var synchrnisation" Watch "Static synchronization vs instance var synchrnisation" New topic
Author

Static synchronization vs instance var synchrnisation

navi shrav
Greenhorn

Joined: Aug 14, 2002
Posts: 1
hi all,
can anybody help me understand as to how a static variable is synchronized verses an instance variable or the difference in the two.
thanks in advance.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi Navi, variables can't be directly synchronized. Only methods and code blocks can be synchronized. Please see the Sun tutorial on synchronization.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It is, however, possible for static methods to be synchronized -- the lock lives in the Class object.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ilya PA
Greenhorn

Joined: Aug 30, 2002
Posts: 4
Hi Navi,
Actually as far as i know you can synchronize a variable. U can do that placing the 'volatile' keyword before a variable's name. This is often used with variables of size bigger then a CPU word size. For example normal PIII word size is 32bits, but variables of type long and double are 64bit vars. So when one thread loads a part of a long var to the cpu(first 32bits), another thread should not be able to access that variable.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Yes but as noted in a rather heated debate over the Behaviour of Volatile variables you will see how Peter Haggar lays out the facts


Normal is in the eye of the beholder
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
If you're talking about synchronizing static methods there is a difference. In this case the class lock is acquired whereas with the non-static method the object lock is acquired.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static synchronization vs instance var synchrnisation
 
Similar Threads
Difference between Static and Instance variable
textbook page 217 error (from the classroom)
Best Practice for getters and setters
Pl explain this a bit diff code
why a transient variable can be declared with static modifier??