Sync Singleton to Java 25: split 6 implementations into separate files, consolidated Main.java demo
This commit is contained in:
16
01-creational/singleton/AppConfigNaiveLazy.java
Normal file
16
01-creational/singleton/AppConfigNaiveLazy.java
Normal file
@@ -0,0 +1,16 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user