Popular Posts

Monday, September 5, 2011

Double-checked locking and the Singleton pattern


                
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}

The theory behind double-checked locking is perfect. Unfortunately, reality is entirely different. The problem with double-checked locking is that there is no guarantee it will work on single or multi-processor machines.
The issue of the failure of double-checked locking is not due to implementation bugs in JVMs but to the current Java platform memory model. The memory model allows what is known as "out-of-order writes" and is a prime reason why this idiom fails.
To show how this occurs, consider the following pseudo code for the line:
instance =new Singleton();
mem = allocate();             //Allocate memory for Singleton object.
instance = mem; //Note that instance is now non-null, but
//has not been initialized.
ctorSingleton(instance); //Invoke constructor for Singleton passing
//instance.


This pseudo code is not only possible, but is exactly what happens on some JIT compilers. The order of execution is perceived to be out of order, but is allowed to happen given the current memory model. The fact that JIT compilers do just this makes the issues of double-checked locking more than simply an academic exercise.

volatile anyone?
Another idea is to use the keyword volatile for the variables inst and instance. According to the JLS (see Resources), variables declared volatile are supposed to be sequentially consistent, and therefore, not reordered. But two problems occur with trying to use volatile to fix the problem with double-checked locking:
The problem here is not with sequential consistency. Code is being moved, not reordered.
                     Many JVMs do not implement volatile correctly regarding sequential consistency anyway.
The solution :
The bottom line is that double-checked locking, in whatever form, should not be used because you cannot guarantee that it will work on any JVM implementation. JSR-133 is addressing issues regarding the memory model, however, double-checked locking will not be supported by the new memory model. Therefore, you have two options:
                Forgo synchronization and use a static field.
class Singleton
{
private Vector v;
private boolean inUse;
private static Singleton instance = new Singleton();

private Singleton()
{
v = new Vector();
inUse = true;
//...
}

public static Singleton getInstance()
{
return instance;
}
}
Refer : http://www.ibm.com/developerworks/java/library/j-dcl/index.html
Java world doc

No comments:

Post a Comment