There isn't any such thing as "static synchronization" or "non-static synchronization". Those ideas hide what's really going on.
This:
class Foobar {
static synchronized void mumble() { ... }
}
Is just a shortcut for writing this:
class Foobar {
static void mumble() {
synchronized(Foobar.class) {
...
}
}
}
And this:
class Foobar {
synchronized void grumble() { ... }
}
Is just a shortcut for writing this:
class Foobar {
void grumble() {
synchronized(this) {
...
}
}
}
It doesn't make sense to talk about "static synchronization" because synchronization is something you do to an object, and there is no such thing as a static object. The only things in Java that can be static
are variables and methods.
P.S., To answer your question,
When a thread enters a synchronized block or method, it must acquire the lock on the specified object if the thread does not already have that object locked. So if f.grumble()
calls mumble()
in my examples above, then the thread first must obtain the lock on f
when it enters the grumble() routine, and then, while still holding that lock, it must also obtain the lock of Foobar.class
.