packageidv.design.pattern.singleton.doublecheck;/** * @author Carl Lu */publicclassSingleton {/* * With volatile, the variable value will not be cached by local thread, * all the read/write operations are against the shared memory so that * we can ensure threads can handle the variable properly. * * This pattern is also called "DCL". */privatevolatilestaticSingleton instance =null;privateSingleton() { }publicstaticSingletongetInstance() {// First checkif (instance ==null) {synchronized (Singleton.class) {// Second checkif (instance ==null) { instance =newSingleton(); } } }return instance; }}