Jump to content

Initialization-on-demand holder idiom: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
-Uncategorized
Line 1: Line 1:
{{Uncategorized|December 2006}}
In [[software engineering]], the '''Initialization on Demand Holder''' idiom ([[design pattern (computer science)|design pattern]]) is a [[lazy initialization|lazy-loaded]] [[Singleton pattern|singleton]]. The idiom can be implemented in both single-threaded/serial and concurrent environments, but care must be taken to correctly implement the idiom under concurrent conditions.
In [[software engineering]], the '''Initialization on Demand Holder''' idiom ([[design pattern (computer science)|design pattern]]) is a [[lazy initialization|lazy-loaded]] [[Singleton pattern|singleton]]. The idiom can be implemented in both single-threaded/serial and concurrent environments, but care must be taken to correctly implement the idiom under concurrent conditions.



Revision as of 19:02, 23 December 2006

In software engineering, the Initialization on Demand Holder idiom (design pattern) is a lazy-loaded singleton. The idiom can be implemented in both single-threaded/serial and concurrent environments, but care must be taken to correctly implement the idiom under concurrent conditions.

Example Java Implementation

This implementation from Bill Pugh (see links below) is a well-performing and concurrent implementation valid in all versions of Java. The original implementation from Bill Pugh has been modified to reduce the scope of LazyHolder.something to package and to make the field final.

 public class Something
 {
     private Something() 
     {
     }
     
     private static class LazyHolder 
     {
         static final Something something = new Something();
     }
     
     public static Something getInstance()
     {
         return LazyHolder.something;
     }
 }
 

How it works

The implementation relies on the well-specified initialization phase of execution within the Java Virtual Machine (JVM); see section 12.4 of Java Language Specification (JLS) for details.

When the class Something is loaded by the Java Virtual Machine (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 something being initialized by executing the (private) constructor for the outer class Something. Since the class initialization phase is guaranteed by the JLS to be serial, 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 something in a serial operation, all subsequent concurrent invocations of the getInstance will return the same correctly initialized something without incurring any additional synchronization overhead.

See also