Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
A {{String}} constant is interned in Java. According to \[[API 06|AA. Java References#API 06]\] Class {{String}} documentation:

When the intern() method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

As a result Consequently, a String constant behaves like a global variable in the JVM. As demonstrated in this noncompliant code example, even if each instance of an object maintains its own field lock, it points to a common String constant in the JVM. Legitimate code that locks on the same String constant will render all synchronization attempts inadequate. Likewise, hostile code from any other package can deliberately exploit this vulnerability.

Code Block
bgColor#FFcccc
// this bug was found in jetty-6.1.3 BoundedThreadPool
private final String _lock = "one";
synchronized(_lock) { /* ... */ }

Noncompliant Code Example

This noncompliant code example synchronizes on a mutable field instead of an object and is bound to demonstrate no mutual exclusion properties, whatsoever. This is because the thread that holds a lock on the field can modify the referenced object's value which in turn will allow allows another thread that is blocked on the old unmodified value to resume, at the same time, granting access to a third thread that is blocked on the modified value. When aiming to modify a field, it is incorrect to synchronize on the same (or another) field as this is equivalent to synchronizing on the field's contents.

Code Block
bgColor#FFcccc
private Integer semaphore = new Integer(0);
synchronized(semaphore) { /* ... */ }

This is a mutual exclusion problem as opposed to the sharing issue discussed in the previous noncompliant code example. Note that only the boxed Integer primitive is shared as shown below and not the Integer object (new Integer(value)) itself.

Code Block
int lock = 0;
Integer Lock = lock;  // boxed primitive Lock will be shared

...

Code Block
bgColor#ccccff
private final Object lock = new Object();
synchronized(lock) { /* ... */ }

Note that the instance of the raw object should not be changed from within the synchronized block. For example, creating and storing the reference of a new object into the lock field.

...

Code Block
bgColor#FFcccc
synchronized(getClass()) { /* ... */ }

Wiki Markup
This idea is sometimes easy to miss, especially when one goes by the Java Language Specification is misunderstood. Section 4.3.2 "The Class Object" of the specification \[[JLS 05|AA. Java References#JLS 05]\] section 4.3.2 "The Class Object", that describes how method synchronization works:

A class method that is declared synchronized synchronizes on the lock associated with the Class object of the class.

This does not mean that it is required to synchronize on the Class object.

Compliant Solution

Explicitly define the name of the class (superclass herein this example) in the synchronization block. This can be achieved in two ways. One way is to explicitly pass the superclass's instance.

...

Code Block
bgColor#ccccff
synchronized(Class.forName("SuperclassName")) { ... }

Finally, it is more important to recognize the entities with which whom synchronization is required rather than indiscreetly scavenging for variables or objects to synchronize on.

...

Wiki Markup
When using synchronization wrappers, the synchronization object needs tomust be the {{Collection}} object. The synchronization is necessary to enforce atomicity ([CON38-J. Ensure atomicity of thread-safe code]). This noncompliant code example demonstrates inappropriate synchronization resulting from locking on a {{Collection}} view instead of the Collection itself \[[Tutorials 08|AA. Java References#Tutorials 08]\]. 

...

Noncompliant Code Example

This noncompliant code example incorrectly uses a ReentrantLock as the lock object.

...