static synchronized void test() { foo(); }
equals
static void test() { synchronized(MyClass.class) { foo(); } }
while
synchronized void test() { foo(); }
equals
void test() { synchronized(this) { foo(); } }
This means: static methods lock on the class object of the class. Non-static methods lock on the instance on which they're called (by default, synchronized(anyOtherLock)
is also possible). Since they lock on different objects, they don't block each other.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…