-
Notifications
You must be signed in to change notification settings - Fork 5
Double Checked Locking_96501859
nxi edited this page Apr 9, 2015
·
1 revision
Created by Tony Lam, last modified on Aug 05, 2008
Double-Checked Locking is a technique for thread-safe object creation within it's owner object. This is useful for avoiding an instance of variable gets created twice in the multi threading environment.
The following example shows how double checked locking works with the singleton pattern:
Note the second check only works with Java 5 or above, due the JVM memory handling model.
// Ensure thread-safe access to this variable private volatile static Singleton uniqueInstance; private Singleton() { super(); } public static Singleton getInstance() { // General check if (uniqueInstance == null) { // Class / static level syncrhonization synchronized (Singleton.class) { // Double check if (uniqueInstance == null) { uniqueInstance = new Singleton(); } } } } }
Document generated by Confluence on Apr 01, 2015 00:11
Home | Developer Guide | Copyright © 2013 ANSTO