They are not synchronized with each other at all. The static method synchronizes on A.class , the second synchronizes on this . So this is (almost) as if you wrote:
public class A { public static void f1() { synchronized(A.class) { ... } } public void f2() { synchronized(this) { ... } } }
and what happens if one protector calls f1 () and f1 () calls f2 ()
Then this thread will own both monitors during f2 . You must be careful before doing this, as if you pulled the locks in reverse order elsewhere, you will get a dead end.
Personally, I would strongly recommend that you completely eliminate synchronous methods. Instead, synchronize on closed end fields that are only used for locking. This means that only your class can acquire the appropriate monitors, so you can talk more carefully about what happens during the lock and avoid deadlocks, etc.
source share