Thread Safe Singleton
A singleton guarantees that a class has exactly one instance and a global point of access to it. Under concurrency the danger is that two threads both find the instance missing and both create it, breaking the single instance promise.
Several proven techniques exist:
- Eager initialization Create the instance when the class loads. The runtime guarantees class initialization happens once, so it is naturally safe, but you pay the cost even if the instance is never used.
- Synchronized accessor Guard the getter with a lock. Correct but slow because every access takes the lock.
- Double checked locking Use a volatile field with a fast unlocked read and a locked slow path for first creation.
- Initialization on demand holder Put the instance in a nested helper class that loads only when first referenced. This gives lazy creation with thread safety provided for free by the class loader.
- Enum singleton In some languages an enum with one constant is the simplest fully safe singleton and also resists serialization tricks.
The core lesson is to lean on guarantees you already have. Class initialization and language level lazy holders provide one time safe construction without any explicit locking, which is why the holder idiom is widely recommended.
Note that singletons also make testing harder and hide dependencies, so use them sparingly.
Key idea
A thread safe singleton relies on a one time construction guarantee such as class initialization or a lazy holder, so two threads can never both build the instance.