Versions Compared

Key

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

...

Compound operations on shared variables are also non-atomic. For more information, see guideline rule VNA02-J. Ensure that compound operations on shared variables are atomic.

Guideline Rule VNA04-J. Ensure that calls to chained methods are atomic describes a specialized case of this guidelinerule.

Noncompliant Code Example (AtomicReference)

...

Wiki Markup
This technique is also called client-side locking \[[Goetz 2006|AA. Bibliography#Goetz 06]\] because the class holds a lock on an object that might be accessible to other classes. Client-side locking is not always an appropriate strategy; see guidelinerule [LCK11-J. Avoid client-side locking when using classes that do not commit to their locking strategy] for more information.

This code does not violate guideline rule LCK04-J. Do not synchronize on a collection view if the backing collection is accessible because, while it does synchronize on a collection view (the synchronizedList), the backing collection is inaccessible and therefore cannot be modified by any code.

...

This compliant solution avoids using Collections.synchronizedMap() because locking on the unsynchronized map provides sufficient thread-safety for this application. Guideline Rule LCK04-J. Do not synchronize on a collection view if the backing collection is accessible provides more information about synchronizing on synchronizedMap objects.

...

Any vulnerabilities resulting from the violation of this guideline rule are listed on the CERT website.

...