Design and Repair a Thread-Safe Configuration Singleton
Diagnose unsafe lazy initialization and implement one safely published configuration instance per application class-loader context within one JVM.
Design and implement a clean Java singleton for immutable application configuration, and repair an unsafe lazy-initialization race. The starting point is a class whose static getInstance method checks whether a static instance is null, constructs it when null, and returns it without synchronization.
Explain the failure when multiple threads call it concurrently.
For this exercise, assume the application uses one class-loader context within one JVM and the singleton provides exactly one instance in that context; its private constructor loads a fixed immutable map exactly once, construction either succeeds completely or throws, and callers only read the resulting configuration.
Use the synchronized keyword in the baseline repair. Do not add distributed coordination, runtime reload, mutable configuration, dependency-container integration, or a test-only reset method.