...
This idiom can also be suitably used by classes designed for inheritance. If a superclass thread requests a lock on the object's monitor, a subclass thread can interfere with its operation. Refer to the guideline CON02-J. Always synchronize on the appropriate object for more details.
An object should use a private internal lock object rather than its own intrinsic lock unless the class can guarantee that untrusted code may not:
...
Likewise, if a static method has the synchronized
keyword, the intrinsic lock of the Class object is obtained, and released when the method completes. The same restrictions listed above apply to static methods, since any untrusted code that can access an object of the class, or a subclass, can use the getClass()
method to obtain access to the Class object. Furthermore, hostile code must not be able to access the Class object at all. This could be accomplished, for instance, by making the class package-private.
This rule can be considered an extension of CON02-J. Always synchronize on the appropriate object. This rule covers method synchronization, which is synchronization on the this
object.
Noncompliant Code Example
...