Y
yaru22
I was reading Wikipedia about Singleton Design pattern and it had some
sample codes for the implementation.
Under Java example implementation, there was "A thread-safe Java
programming language lazy-loaded solution." But I don't understand why
it is thread-safe & lazy-loaded solution. Could anyone explain it?
This is the source code from the Wikipedia (http://en.wikipedia.org/
wiki/Singleton_pattern) :
public class Singleton
{
// Private constructor suppresses generation of a (public) default
constructor
private Singleton() {}
private static class SingletonHolder
{
private final static Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance()
{
return SingletonHolder.INSTANCE;
}
}
Thank you.
sample codes for the implementation.
Under Java example implementation, there was "A thread-safe Java
programming language lazy-loaded solution." But I don't understand why
it is thread-safe & lazy-loaded solution. Could anyone explain it?
This is the source code from the Wikipedia (http://en.wikipedia.org/
wiki/Singleton_pattern) :
public class Singleton
{
// Private constructor suppresses generation of a (public) default
constructor
private Singleton() {}
private static class SingletonHolder
{
private final static Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance()
{
return SingletonHolder.INSTANCE;
}
}
Thank you.