singleton pattern in java- lazy Intialization

Posted by flash on Stack Overflow See other posts from Stack Overflow or by flash
Published on 2010-03-26T08:29:44Z Indexed on 2010/03/26 8:33 UTC
Read the original article Hit count: 156

public static MySingleton getInstance() {
 if (_instance==null) {
   synchronized (MySingleton.class) {
      _instance = new MySingleton();
   }
 }
 return _instance;
} 

1.is there a flaw with the above implementation of the getInstance method? 2.What is the difference between the two implementations.?

public static synchronized MySingleton getInstance() { 
 if (_instance==null) {
  _instance = new MySingleton();
 }

 return _instance;
} 

I have seen a lot of answers on the singleton pattern in stackoverflow but the question I have posted is to know mainly difference of 'synchronize' at method and block level in this particular case.

© Stack Overflow or respective owner

Related posts about java

Related posts about singleton