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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
// "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
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}}. \[[Pugh 04|AA. Java References#Pugh 04]\] This compliant solution declares the Wiki Markup Helper
object as volatile
.
Code Block | ||
---|---|---|
| ||
// 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.
...