...
When explicitly constructed, an Integer
object has a unique reference and its own intrinsic lock that is not shared with other Integer
objects or boxed integers having the same value. While this is an acceptable solution, it can cause maintenance problems because developers can incorrectly assume that boxed integers are appropriate lock objects. A more appropriate solution is to synchronize on an internal a private final lock Object
as described in the following compliant solution.
...
A String
instance differs from a String
literal. The instance has a unique reference and its own intrinsic lock that is not shared by other string object instances or literals. A better approach is to synchronize on an internal a private final lock object as shown in the following compliant solution.
Compliant Solution (
...
Private Final Lock Object
)
This compliant solution synchronizes on an internal a private final lock object. This is one of the few cases where a java.lang.Object
instance is useful.
...