public class MySingletonClass {
private static MySingletonClass instance;
private MySingletonClass(){
//private constructor to prevent extra instantiation
}
public static MySingletonClass getInstance(){
if(instance == null) {
instance = new MySingletonClass();
}
return instance;
}
// rest of MySingletonClass's methods...
}
public class MySingletonClass {
private static MySingletonClass instance;
private MySingletonClass(){
//private constructor to prevent extra instantiation
}
public static MySingletonClass getInstance(){
if(instance == null) {
synchronized(MySingletonClass.class){
if(instance == null){
instance = new MySingletonClass();
}
}
}
return instance;
}
// rest of MySingletonClass's methods...
}
//Lazy initializing method
public class MySingletonClass {
private static class LazyInitializer {
private static MySingletonClass instance = new MySingletonClass();
}
}
public static MySingletonClass getInstance(){
return LazyInitializer.instance;
}
public class MySingletonClass {
private static class LazyInitializer{
private static MySingletonClass instance = new MySingletonClass();
static {
instance.setMyParam("A_PARAM_VALUE");
}
}
}
//Lazy initializing method
public class MySingletonClass {
private MySingletonClass(){
//private constructor to prevent extra instantiation
}
private static LazyInitializer {
private static MySingletonClass instance = new MySingletonClass();
}
public static MySingletonClass getInstance(){
return LazyInitializer.instance;
}
// rest of MySingletonClass's methods...
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (6)
Commented:
Good read. However, i was thinking will LazyInitializer.instance always be created? Wonder if for some reason you didn't want the instance to be created until getInstance is called for the first time to save memory or something. Although, I can't really think of a reason why you'd want to do this. Why would you import a class that you don't use or have an empty instance lying around? I guess this way is really the only way though to prevent multiple thread problems like you said.
Author
Commented:to WhiteMage, yes you're right a 'class' is missed there sorry for that and thank you to comment it
Why we use this, because recent machines have more than one CPU. I am not very expert in this area but as far as I know these multi-cpu machines have many feature for better performance so guys who developed new JVMs (since version 5 I think) have changed the memory management in order to use the most of it. In this way the old-fashioned singleton patterns won't work! Maybe you have no problem with your personal computer even with hundred threads but in a server machine you will. I had this experience once and I was wondered how this singleton turn out to two instances!!!
Regarding the unwanted instance I guess (not sure but I read somewhere a long time ago) that since this is an inner class so JVM will not load it unless a first access to it
Commented:
I just wanted to discuss is it only the object creation (lazy initialization) that drives you to make inner class and put the singleton instance in it. I mean what if we have the following code
public class MySingletonClass {
private static MySingletonClass instance = new MySingletonClass();
private MySingletonClass(){
//private constructor to prevent extra instantiation
}
public static MySingletonClass getInstance(){
return instance;
}
// rest of MySingletonClass's methods...
}
Now i know that here the singleton instance will be created alongwith the class being loaded. But there wont be any multithreaded issues. right
So can i say that if i need my singleton instance at some delayed point in time then i can use your version and if its instantly needed (at first reference of the class) then i can use above ?
Author
Commented:The main reason for lazy initialization is that this instance may not be used for a long time, so it's better not to load it until then
Commented:
View More