Versions Compared

Key

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

...

Code Block
bgColor#ccccff
langjava
public final class Helper {
  private final int n;
 
  public Helper(int n) {
    this.n = n;
  }
 
  // Other fields and methods, all fields are final
}
 
final class Foo {
  private Helper helper = null;
 
  public Helper getHelper() {
    Helper h = helper;       // Only unsynchronized read of helper
    if (h == null) {
      synchronized (this) {
        h = helper;          // In synchronized block, so this is safe
        if (h == null) {
          h = new Helper(42);
          helper = h;
        }
      }
    }
    return h;
  }
}

Exceptions

LCK10-J-EX0: Use of the noncompliant form of the double-checked locking idiom is permitted for 32-bit primitive values (for example, int or float) [Pugh 2004], although this usage is discouraged. The noncompliant form establishes the necessary happens-before relationship between threads that see an initialized version of the primitive value. The second happens-before relationship (for the initialization of the fields of the referent) is of no practical value because unsynchronized reads and writes of primitive values up to 32-bits are guaranteed to be atomic. Consequently, the noncompliant form establishes the only needed happens-before relationship in this case. Note, however, that the noncompliant form fails for long and double because unsynchronized reads or writes of 64-bit primitives lack a guarantee of atomicity and consequently require a second happens-before relationship to guarantee that all threads see only fully assigned 64-bit values (see  VNA05-J. Ensure atomicity when reading and writing 64-bit values for more information).

...

Using incorrect forms of the double-checked locking idiom can lead to synchronization problems and can expose partially initialized objects.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

LCK10-J

Low

Probable

Medium

P4

L3

Automated Detection

Tool
Version
Checker
Description
CodeSonar

Include Page
CodeSonar_V
CodeSonar_V

JAVA.CONCURRENCY.LOCK.DCLDouble-Checked Locking (Java)
Coverity7.5

DOUBLE_CHECK_LOCK
FB.DC_DOUBLECHECK

Implemented
Parasoft Jtest
9.5TRS.DCL
Include Page
Parasoft_V
Parasoft_V
CERT.LCK10.DCLAvoid unsafe implementations of the "double-checked locking" pattern
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V5304, V6082
SonarQube
Include Page
SonarQube_V
SonarQube_V
S2168
Implemented

Related Guidelines

MITRE CWE

CWE-609, Double-checked Locking

Bibliography

 



...

Image Modified Image Modified Image Modified