Skip to content

Double checked locking technique for Java_127926469

nxi edited this page Apr 9, 2015 · 1 revision

Gumtree : Double-checked locking technique for Java

Created by Tony Lam, last modified on Aug 17, 2009
This technique is used to avoid unnecessary object creation in the multi-threading environment when lazy instantiation is used.
// Works with acquire/release semantics for volatile
// Broken under Java 1.4 and earlier semantics for volatile
class Foo {
    private volatile Helper helper = null;
    public Helper getHelper() {
        if (helper == null) {
            synchronized(this) {
                if (null == helper)
                    helper = new Helper();
            }
        }
        return helper;
    }
}
References:
Document generated by Confluence on Apr 01, 2015 00:11
Clone this wiki locally