Name: Anonymous 2009-01-09 5:18
A toast for one of my favorite ways to create and interact with things. I love you, singleton. And there's only one of you.
enterpriseSingleton is null, and begin instantiating it. Before the instantiation has finished, another thread may observe the enterpriseSingleton as being non-null, and so attempt to use the object when it is only partly initialized.public class EnterpriseSingleton {
private static EnterpriseSingleton enterpriseSingleton;
private EnterpriseSingleton() {
}
public static EnterpriseSingleton getInstance() {
if (enterpriseSingleton == null) {
EnterpriseSingleton temp;
synchronized (EnterpriseSingleton.class) {
if (enterpriseSingleton == null) {
temp = new EnterpriseSingleton();
}
synchronized (EnterpriseSingleton.class) {
enterpriseSingleton = temp;
}
}
}
return enterpriseSingleton;
}
}