In
software engineering
Software engineering is a systematic engineering approach to software development.
A software engineer is a person who applies the principles of software engineering to design, develop, maintain, test, and evaluate computer software. The term '' ...
, the initialization-on-demand holder (
design pattern
A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
) idiom is a
lazy-loaded singleton
Singleton may refer to:
Sciences, technology Mathematics
* Singleton (mathematics), a set with exactly one element
* Singleton field, used in conformal field theory Computing
* Singleton pattern, a design pattern that allows only one instance ...
. In all versions of Java, the idiom enables a safe, highly concurrent lazy initialization of static fields with good performance.
public class Something
The implementation of the idiom relies on the initialization phase of execution within the
Java Virtual Machine
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
(JVM) as specified by the Java Language Specification (JLS). When the class
Something
is loaded by the JVM, the class goes through initialization. Since the class does not have any static variables to initialize, the initialization completes trivially. The static class definition
LazyHolder
within it is ''not'' initialized until the JVM determines that
LazyHolder
must be executed. The static class
LazyHolder
is only executed when the static method
getInstance
is invoked on the class
Something
, and the first time this happens the JVM will load and initialize the
LazyHolder
class. The initialization of the
LazyHolder
class results in static variable
INSTANCE
being initialized by executing the (private) constructor for the outer class
Something
. Since the class initialization phase is guaranteed by the JLS to be sequential, i.e., non-concurrent, no further synchronization is required in the static
getInstance
method during loading and initialization. And since the initialization phase writes the static variable
INSTANCE
in a sequential operation, all subsequent concurrent invocations of the
getInstance
will return the same correctly initialized
INSTANCE
without incurring any additional synchronization overhead.
Caveats
While the implementation is an efficient thread-safe "singleton" cache without synchronization overhead, and better performing than uncontended synchronization,
the idiom can only be used when the construction of
Something
is guaranteed to not fail. In most JVM implementations, if construction of
Something
fails, subsequent attempts to initialize it from the same class-loader will result in a
NoClassDefFoundError
failure.
See also
*
Singleton pattern
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. One of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems ...
*
Double-checked locking In software engineering, double-checked locking (also known as "double-checked locking optimization") is a software design pattern used to reduce the overhead of acquiring a lock by testing the locking criterion (the "lock hint") before acquiring th ...
*
Multiton pattern
In software engineering, the multiton pattern is a design pattern which generalizes the singleton pattern. Whereas the singleton allows only one instance of a class to be created, the multiton pattern allows for the controlled creation of multip ...
External links
* http://www.cs.umd.edu/~pugh/java/memoryModel/
* http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
* http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
References
{{reflist
Software design patterns
Articles with example Java code