Versions Compared

Key

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

...

Code Block
bgColor#ccccff
// Works with acquire/release semantics for volatile
// Broken under JDK 1.4 and earlier
class Foo {
  private volatile Helper helper = null;
  
  public Helper getHelper() {
    if (helper == null) {
      synchronized(this) {
        if (helper == null) {
          helper = new Helper();
        }
      }
    }
    return helper;
  }
}

Exceptions

EX1: Explicitly synchronized code does not require the use of double-checked locking.

Wiki Markup
*EX2:* "Although the double-checked locking idiom cannot be used for references to objects, it can work for 32-bit primitive values (e.g., int's or float's). Note that it does not work for long's or double's, since unsynchronized reads/writes of 64-bit primitives are not guaranteed to be atomic." \[[Pugh 04|AA. Java References#Pugh 04]\]

Risk Assessment

Using incorrect forms of the double checked locking idiom can lead to synchronization issues.

...