Possible Duplicate:
Efficient way to implement singleton pattern in Java
I would have thought that class below is a thread safe singleton but reading http://taskinoor.wordpress.com/2011/04/18/singleton_multithreaded/ it seems it is not.
public class ThreadSafeSingleton {
private static ThreadSafeSingleton ref;
private ThreadSafeSingleton(){
}
public ThreadSafeSingleton getSingletonObject(){
if(ref == null){
ref = new ThreadSafeSingleton();
}
return ref;
}
}
According to the article the only truly thread safe singleton is -
public class ThreadSafeSingleton {
private static ThreadSafeSingleton ref = new ThreadSafeSingleton();
private ThreadSafeSingleton(){
}
public ThreadSafeSingleton getSingletonObject(){
return ref;
}
}
Is this correct ?