Misuse of synchronization primitives is a common source of concurrency issues. Synchronizing on objects that may be reused can result in deadlock and nondeterministic behavior.
Noncompliant Code Example (
Boolean Lock Object)
This noncompliant code example synchronizes on a
Boolean lock object.
Boolean type is unsuitable for locking purposes because it allows only two values:
FALSE. Boolean literals containing the same value share unique instances of the
Boolean class in the JVM. In this example,
initialized references the instance corresponding to the value
FALSE. If any other code inadvertently synchronizes on a
Boolean literal with the value
FALSE, the lock instance is reused and the system can become unresponsiveness or deadlocked.
Noncompliant Code Example (Boxed Primitive)
This noncompliant code example locks on a boxed
Boxed types may use the same instance for a range of integer values and consequently suffer from the same problem as
Boolean constants. If the value of the primitive can be represented as a byte, the wrapper object is reused. Note that the use of the boxed
Integer wrapper object is insecure; instances of the
Integer object constructed using the
new operator (
new Integer(value)) are unique and not reused. In general, holding a lock on any data type that contains a boxed value is insecure.
Compliant Solution (Integer)
This compliant solution recommends locking on a non-boxed
doSomething() method synchronizes using the intrinsic lock of the
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 a private final lock
Object as described in the following compliant solution.
Noncompliant Code Example (Interned
This noncompliant code example locks on an interned
According to the Java API class [[API 2006]]
intern()method is invoked, if the pool already contains a string equal to this
Stringobject as determined by the
equals(Object)method, then the string from the pool is returned. Otherwise, this
Stringobject is added to the pool and a reference to this
Stringobject is returned.
Consequently, an interned
String object behaves like a global variable in the JVM. As demonstrated in this noncompliant code example, even if every instance of an object maintains its own
lock field, the field references a common
String constant. Locking on
String constants has the same problem as locking on
Additionally, hostile code from any other package can exploit this vulnerability, if the class is accessible. (For more information, see guideline LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code.)
Noncompliant Code Example (
This noncompliant code example locks on a final
String literal is a constant and interned. Consequently, it suffers from the same pitfalls as the preceding noncompliant code example.
Compliant Solution (
This compliant solution locks on a
String instance that is not interned.
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 a private final lock object as shown in the following compliant solution.
Compliant Solution (Private Final Lock
This compliant solution synchronizes on a private final lock object. This is one of the few cases where a
java.lang.Object instance is useful.
For more information on using an
Object as a lock, see guideline LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code.
A significant number of concurrency vulnerabilities arise from locking on the wrong kind of object. It is important to consider the properties of the lock object rather than indiscreetly scavenging for objects to synchronize on.
[[API 2006]] Class String, Collections
[[Miller 2009]] Locking
[[Pugh 2008]] "Synchronization"
[[Tutorials 2008]] Wrapper Implementations