Implementation of Singleton Pattern in multi-threading applicationSingleton Pattern is used to restrict the creation of multiple instance of a class because presence of multiple instances sometimes can crash your system. However it ensures the class will have only one instance and global access. here is a solution which uses lazy initialization.
public class SingletonPattern {
private static SingletonPattern instance = null;
public static Object lock = new Object();
private SingletonPattern(){}
public static SingletonPattern getConnection() {
synchronized (lock) {
if (instance == null) {
try {
instance=new SingletonPattern();
} catch (Exception ex) {
ex.printStackTrace();
}finally{
}
}
}
return instance;
}
}
===================================================================================================