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.
.getInst() instantiation faggotry.
public class EnterpriseSingleton extends java.lang.Object {
private static EnterpriseSingleton _enterpriseSingleton;
private EnterpriseSingleton() {
/* Empty constructor */
}
/**
* Enterprise thread safety
*/
public static EnterpriseSingleton getInstance() {
if (_enterpriseSingleton == null) {
synchronized (EnterpriseSingleton.class) {
_enterpriseSingleton = new EnterpriseSingleton();
}
}
return _enterpriseSingleton;
}
}
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;
}
}