I want to understand how locking is done on static methods in Java.
let's say I have the following class:
class Foo {
private static int bar = 0;
public static synchronized void inc() { bar++; }
public synchronized int get() { return bar; }
It's my understanding that when I call f.get()
, the thread acquires the lock on the object f
and when I do Foo.inc()
the thread acquires the lock on the class Foo
.
My question is how are the two calls synchronized in respect to each other? Is calling a static method also acquires a lock on all instantiations, or the other way around (which seems more reasonable)?
EDIT:
My question isn't exactly how static synchronized
works, but how does static and non-static methods are synchronized with each other.
i.e., I don't want two threads to simultaneously call both f.get()
and Foo.inc()
, but these methods acquire different locks. My question is how is this preventable and is it prevented in the above code.
Static and instance
synchronized
methods are not related to each other, therefore you need to apply some additional synchronization between them, like this:(though in this case leaving
synchronized
onget()
doesn't make sense, since it doesn't do anything that requires synchronization on instance).Beware of deadlocks - since this code aquires multiple locks, it should do it in consistent order, i.e. other synchronized static methods shouldn't try to acquire instance locks.
Also note that this particular task can be solved without synchronization at all, using atomic fields:
A synchronized static method is effectively equivalent to:
In other words, it locks on the
Class
object associated with the class declaring the method.From section 8.4.3.6 of the JLS:
Neither, the non-static synchronized call does not acquire a lock on the class itself. (And the static synchronized block does not lock any object instantiated from that class.)
In other words the calls
f.get()
(locksf
) andFoo.inc()
(locks the classFoo
) can run concurrently. They are not "synchronized".You could use a different pattern (singleton), or make all the methods static.
If you read http://download.oracle.com/javase/tutorial/essential/concurrency/locksync.html.
It will tell you:
which tells you all you need to know.
Static
locks are attached to theclass
definition and thus is shared between all instances of that class.Synchronization of
none static
methods only apply to the current instance of the class (the lock is on the class instance, e.g.,this
). In your example you have two different locks with no interrelation.You must share a lock to be able to arbitrate access to both
f.get
andFoo.inc()
. You can do this either by sharing the same static lock or by the same instance lock.These two calls do not synchronize in respect to each other. It is as you said, a caller of
f.get()
acquires the lock off
object and caller ofFoo.inc()
acquiresFoo.class
object's one. So the synchronization rules are the same as if instead of static call you called an instance synchronized method with another object.