單例模式懶加載

public class Something {
	private Something() {}
 
	private static class LazyHolder {
		private static final Something INSTANCE = new Something();
	}
 
	public static Something getInstance() {
		return LazyHolder.INSTANCE;
	}
}
The implementation of the idiom relies on the well-specified initialization phase of execution within the Java Virtual Machine (JVM).[2] 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 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 INSTANCE in a serial operation, all subsequent concurrent invocations of the getInstancewill return the same correctly initialized INSTANCE without incurring any additional synchronization overhead.
來源  : http://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章