What is the difference between these two approaches of creating singleton Class ?<br /> <br /> //Double Checked Locking Code<br /> public static Singleton createInstance() {<br />    if(singleton == null){<br />       synchronized(Singleton.class) { <br />          if(singleton == null) {<br />             singleton = new Singleton();<br />          }<br />       }<br />    }<br />    return singleton;<br /> }<br /> <br /> //Single checked locking code<br /> public static Singleton createInstance() {<br />    synchronized(Singleton.class) { <br />       if(singleton == null) {<br />          singleton = new Singleton();<br />       }<br />    }<br />    return singleton;<br /> }

Search Interview Questions

 3356 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text


Java - Interview Questions and Answers

 Q1. What is the difference between these two approaches of creating singleton Class ?

//Double Checked Locking Code
public static Singleton createInstance() {
   if(singleton == null){
      synchronized(Singleton.class) {
         if(singleton == null) {
            singleton = new Singleton();
   return singleton;

//Single checked locking code
public static Singleton createInstance() {
   synchronized(Singleton.class) {
      if(singleton == null) {
         singleton = new Singleton();
   return singleton;
Ans. In First Case , Lock for the synchronized block will be received only if singleton == null whereas in second case every thread will acquire the lock before executing the code.
The problem of synchronization with singleton will only happen when the object has not be instantiated. Once instantiated , the check singleton == null will always generate true and the same object will be returned and hence no problem. First condition will make sure that synchronized access ( acquiring locks ) will only take place if the object has not been created so far.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   singleton   synchronization

Try 1 Question(s) Test

Related Questions

 Difference between Static and Singleton Class ?
 What is the difference between Singleton and Prototype Bean scope in Spring ?
  When should we use prototype scope and singleton scope for beans ?
  Explain Singleton Design Pattern ?
  Shouldn't we make a class with all static members is its just expected to be executed as a standalone program with just one thread. Moreover Lets assume that there is no runtime Polymorphism required and there is no need for serialization ?
  Should we have instance variables in the Spring Bean which has been scoped as Singleton ?
  What are stateless objects ? How are they different from immutable objects ? Which of these two is thread safe ?
  Action Class is singleton and hence not thread safe in ..
  Which of the following is not the difference between Singleton and Static class ( Class with static members only ) ?

Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner

comments powered by Disqus

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:

X Close this