Double Checked Locking - Singleton Pattern

May 13, 2018 by Sandeep Bhardwaj | Tags:


Singleton.java

public class Singleton {
	private static volatile Singleton _instance; // volatile variable
	private Singleton(){} //private constructor

	public static Singleton getInstance() {
		if (_instance == null) {
			synchronized (Singleton.class) {
				if (_instance == null)
					_instance = new Singleton();
			}
		}
		return _instance;
	}
}