File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Synchronizing Static Methods 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 "Synchronizing Static Methods" Watch "Synchronizing Static Methods" New topic
Author

Synchronizing Static Methods

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I have this question, is it possible to synchronize static methods by just using the keyword synchronised like so..
" public static synchronized void doStuff() {} ". If it is then how is it determined which of the several threads accessing doStuff() gets the lock ?
thanks
gautam
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Yes, it's possible. There is a Class object (an instance of the class Class) which represents the class which owns the static method. All syncronized static methods of a given class compete for a lock on that Class object, rather than on the current ("this") instance as they would if they were non-synchronized. Thus
<code><pre>public static synchronized void doStuff() {
...
}</pre></code>
Is equivalent to:
<code><pre>public static void doStuff() {
synchronized (classObject) {
...
}
}</pre></code>
So no two static synchronized methods of the same class (defined in the same class, not inherited) can ever run at the same time.
[This message has been edited by Jim Yingst (edited September 19, 2000).]


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Synchronizing Static Methods
 
Similar Threads
Overriden VS Redefinition
varargs doubt
static overriding
Overridding Doubt
static methods-overridden