In the case of
public void synchronized f(){...}
The synchronization is per instance of the of enclosing class. This means that multiple threads can call
f
on different instances of the class.
For
public static void synchronized f(){...}
Only one thread at a time can call that method, regardless of the number of instances of the enclosing class.
Technically, the monitor taken by
synchronized
in the first example is the of the object instance and the monitor take in the second example is that of the Class
object.
Note that, if you have classes of the same name in different
ClassLoaders
, the do not share the same monitor, but this is a detail you're unlikely to encounter.
No comments:
Post a Comment