Versions Compared

Key

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

The double checked locking idiom is sometimes used to provide lazy initialization in multithreaded code. The code shown below ensures that only one instance of the Helper object can exist at a particular time in a multithreaded context. In a multi-threading scenario, lazy initialization refers to reducing the cost of synchronization on each method access by deferring the synchronization to the moment when the object is actually initialized.

The code shown below is correctly synchronized, albeit slower. The double-checked locking pattern strives to make it faster.

Code Block
bgColor#ccccff
Code Block
// Correct multithreaded version using synchronization
class Foo { 
  private Helper helper = null;
  
  public synchronized Helper getHelper() {
    if (helper == null) {
      helper = new Helper();
    }
    return helper;
  }
  // Other functions and members...
}

This code ensures that in a multithreaded context, only one instance of the Helper object can exist at a particular time. The double checked locking idiom eliminates the synchronization need to synchronize every time the getHelper() method is invoked, to achieve performance gains. If implemented incorrectly, it may offer no such benefits and lead to erroneous or ineffective synchronization.

...

Code Block
bgColor#FFCCCC
// "Double-Checked Locking" idiom
class Foo { 
  private Helper helper = null;
  public Helper getHelper() {
    if (helper == null) { 
      synchronized(this) {
        if (helper == null) 
          helper = new Helper();
      }    
    }
    return helper;
  }
  // other functions and members...
}

Compliant Solution

Wiki MarkupJDK 5.0 allows a write of a {{volatile}} variable to be reordered with respect to a previous read or write. A read of a {{volatile}} variable cannot be reordered with respect to any following read or write. Because of this, the double checked locking idiom can work when {{helper}} is declared {{volatile}}. \[[Pugh 04|AA. Java References#Pugh 04]\] This compliant solution declares the Helper object as volatile.

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(); // If the helper is null, create a new instance
        }
      }
    }
    return helper; // If helper is non-null, return its instance
  }
}

Wiki Markup
JDK 5.0 allows a write of a {{volatile}} variable to be reordered with respect to a previous read or write. A read of a {{volatile}} variable cannot be reordered with respect to any following read or write. Because of this, the double checked locking idiom can work when {{helper}} is declared {{volatile}}. If a thread initializes the {{Helper}} object, a happens-before relationship is established between this thread and another that retrieves and returns the instance. \[[Pugh 04|AA. Java References#Pugh 04]\] and \[[Manson 04|AA. Java References#Manson 04]\] 

Exceptions

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

...