Hi,
I understand the following class is an example of thread safe implementation of Singleton.
This class initializes the instance lazily and in a thread safe manner. The drawback being that parameters cannot be passed into the getInstance method to construct the instance.
Can someone please guide me on how to achieve (1) Thread safe singleton and (2) Lazy initialization of the instance and (3) Parameterizing the getInstance method.
I've tried searching on the web for some guidance on this... All that I can find are either "DCL is broken" or the following implementation.
Thank you.
public class Singleton {
private Singleton() {}
public static Singleton getInstance() {
return SingletonHolder.getInstance();
}
private static class SingletonHolder {
private static Singleton s_instance;
static {
s_instance = new Singleton();
}
public static Singleton getInstance() {
return s_instance;
}
}
}