get in touch
>

LIKE

SHARE

Facebook Share Tweet LinkedIn Share

FOLLOW

LinkedIn Follow Xing Follow
Lucian Neghina

Software Developer at ASSIST

​Everyone knows what is singleton pattern, but how to implement a singleton class in multithreaded environment? Next, I’m going to present best approaches for this use case.

Bill Pugh Singleton Implementation

public class Singleton {
	private Singleton() { }

	private static class SingletonHolder {
		private static final Singleton INSTANCE = new Singleton();
	}

	public static Singleton getInstance() {
		return SingletonHolder.INSTANCE;
	} 
}

Thread Safe Double Checked Singleton Implementation

public class Singleton {
	private static Singleton instance;

	private Singleton() { }

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

Do you want to get in touch with us? 

If you are interested in our software development services, you would like to join our team, or you simply want to find out more about us, we’d love to hear from you! Drop us a line and a member of the ASSIST team will get back to you as soon as possible. We are sure we can ASSIST you.

GET IN TOUCH