Versions Compared

Key

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

Static shared data should not be protected using instance locks because these are ineffective when two or more instances of the class are created. Consequently, the shared state is not safe for concurrent access unless a static lock object is used. If the class can interact with untrusted code, the lock must also be private and final, as in guideline rule LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code.

...

This example does not prevent either thread from observing an inconsistent value of counter because the increment operation on volatile fields is not atomic in the absence of proper synchronization. (See guideline rule VNA02-J. Ensure that compound operations on shared variables are atomic.)

...

Search for vulnerabilities resulting from the violation of this guideline rule on the CERT website.

Bibliography

...