Files
design-patterns/01-creational/singleton/AppConfigNaiveLazy.java

17 lines
617 B
Java

package singleton;
public class AppConfigNaiveLazy {
private static AppConfigNaiveLazy instance; // null until first request
private AppConfigNaiveLazy() { /* load config */ }
// BROKEN IN MULTITHREADED CODE — see explanation below
public static AppConfigNaiveLazy getInstance() {
if (instance == null) { // Thread A: checks, sees null
instance = new AppConfigNaiveLazy(); // Thread B: also sees null, also enters here
} // Both threads create a new AppConfigNaiveLazy!
return instance;
}
}