S
swan2030
I saw two ways of instantiate singleton.
Somebody can help me compare those two. ( with multi-thread in mind).
1)
private static final MyClass singleton = new MyClass();
public static MyClass getInstance()
{
return singleton;
}
2) Lazy and thread safe
static private class Holder
{
static protected final MyClass instance= new MyCLass();
}
public static MyClass getInstance()
{
return MyClass.Holder.instance;
}
Thanks,
Somebody can help me compare those two. ( with multi-thread in mind).
1)
private static final MyClass singleton = new MyClass();
public static MyClass getInstance()
{
return singleton;
}
2) Lazy and thread safe
static private class Holder
{
static protected final MyClass instance= new MyCLass();
}
public static MyClass getInstance()
{
return MyClass.Holder.instance;
}
Thanks,