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
posted
0
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.