• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing Static Methods

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!