Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added NCE/CS

...

Noncompliant Code Example

This noncompliant code example locks on a non-final object that is declared public. It is possible that untrusted code can change the value of the lock object and foil the attempt to synchronize.

Code Block
bgColor#FFcccc

public Object publicLock = new Object();
synchronized(publicLock) { 
  // body
}

Compliant Solution

This compliant solution synchronized on a private object and is safe from malicious manipulation.

Code Block
bgColor#ccccff

private final Object privateLock = new Object();
synchronized(privateLock) { 
  // body
}

Noncompliant Code Example

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

...

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 allows another thread that is blocked on the 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.

...

In general, holding a lock on any data structure that contains a boxed value can be dangerous.

Compliant Solution

In the absence of an existing object to lock on, using a raw object to synchronize suffices.

...

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.

Noncompliant Code Example

Synchronizing on getClass() rather than a class literal can also be counterproductive. Whenever the implementing class is subclassed, the subclass will end up locking on a completely different Class object.

...

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

Compliant Solution

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

...

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

Noncompliant Code Example

Wiki Markup
When using synchronization wrappers, the synchronization object must 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]\]. 

Code Block
bgColor#FFcccc
Map<Integer, String> m = Collections.synchronizedMap(new HashMap<Integer, String>());
Set<Integer> s = m.keySet();
synchronized(s) {  // Incorrectly synchronizes on s
  for(Integer k : s) { /* do something */ }
}

Compliant Solution

This compliant solution correctly synchronizes on the Collection object instead of the Collection view.

Code Block
bgColor#ccccff
// ...
synchronized(m) {  // Synchronize on m, not s
  for(Integer k : s) { /* do something */ }
}

Noncompliant Code Example

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

Code Block
bgColor#FFcccc
final Lock lock = new ReentrantLock();
synchronized(lock) { /* do something */ }

Compliant Solution

The proper mechanism to lock in this case is to explicitly use the lock() and unlock() methods provided by the ReentrantLock class.

...