Double-Checked Locking

I read this paper on double-checked locking. It had a great discussion of the pitfalls of using this approach for implementing the Singleton Pattern. Here is a typical example of an unsafe implementation:

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

Leave a Reply

Your email address will not be published. Required fields are marked *