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.
The moose likes Threads and Synchronization and the fly likes static member's synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "static member Watch "static member New topic
Author

static member's synchronization

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

Hi all,,

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
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static member's synchronization
 
Similar Threads
Threads And Synchronization
Static Method
can we override the static methods ?
Why finder methods are not static?
Interview Questions