This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
can we synchronization static methods blocks..etc? if not.. why we can not do on it. is it just because we static variables and methods are do not belong to class?
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
1
Static methods and variables belong to the class, not any particular instance of the class. So a synchronized static method synchronizes on the class itself. A thread must get the monitor on the class before entering the method.
A block synchronized on a static variable gives roughly the same effect; a thread must get the monitor for the object referenced by that variable before entering the block. No matter how many instances there are, they are all synchronized on the same object.
Does that make sense?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
posted
1
Originally posted by Stan James:
A block synchronized on a static variable gives roughly the same effect; a thread must get the monitor for the object referenced by that variable before entering the block. No matter how many instances there are, they are all synchronized on the same object.
With the unstated assumption that the value of the variable hasn't been changed to point at a different object.