JavaMemoryModel: Will this solution work?

From: Srinivas_Kunani@toyota.com
Date: Tue Sep 16 2003 - 23:48:46 EDT


I am trying to get an understanding of the subtleties of the Double-Check
locking problem in Java

Will the following code resolve the problem. If it will not, can somebody
please explain why not.

Thanks in advance for your responses.

Srinivas.

...
private static Singleton instance;
private static Object mutex = new Object();
private static boolean initialized = false;

private Singleton() {
}

public static Singleton getInstance() {
if (instance == null || ! initialized) {
    synchronized(mutex) {
         if (instance == null ) {
             instance = new Singleton();
             initialized = (instance.getClass() != null);
         }
    }
}
return instance;
}
...

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:51 EDT