-1

To ensure only one instance of the class get created in a multi-threaded environment, we create a singleton class with the below structure.

class Singleton {

    private volatile static Singleton _instance;

    private Singleton() {
        // preventing Singleton object instantiation from outside
    }

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

But is there a way we can improve the performance of the class by getting ride of the penalty that every thread has to pay while doing a null check before getting the object from the static function ?

Arpan
  • 913
  • 2
  • 12
  • 19
  • 7
    Null checks are really cheap. This is not something I would worry about optimizing, even if it were possible to optimize, which I doubt. In particular, CPUs are _great_ at optimizing if checks that almost always go the same way, which this one will. This is basically free and you should _really_ find something else to worry about. – Louis Wasserman Apr 01 '16 at 18:55
  • Can't you just statically initialize `_instance` instead of creating it on first access? That way you don't need to check for null on each access. – Joachim Isaksson Apr 01 '16 at 18:58
  • See Effective Java second Edition – AlexWien Apr 01 '16 at 19:00

3 Answers3

3

This is known as premature optimization. You don't actually know that the null check is causing a problem (it's not, trust me). The cost of a null check is on the order of nanoseconds.

Worry about performance only after you identify that there actually is a real problem. Then, do some profiling to identify the sections of code that are causing the problem, do not optimize things that "look" like they might be a problem without performance statistics to back up that perception.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Thank @Jim even i know the same thing that it will not hit the performance . but this was one of a question asked by one of a very senior developer in an interview , which I was not able to address it . – Arpan Apr 01 '16 at 19:04
2

According to Josh Bloch. Effective Java Second Edition since Java 1.5 this is the best approach

public enum Singelton {
  INSTANCE;
}

It is safe even against sophisticated serialisation or reflection attacks.

But always keep in mind that Singletons defend testatibility. While the Singleton in your question can be reset for test purposes using reflection, the one in my answer probably cannot.

So I recommend to think twice whether a Singleton is needed, I always avoid them.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

The best approach for a Singleton is to instantiate early unless there is some reason for resource constraints.

public class Singleton
{
   //guaranteed thread safe
   private static final Singleton _instance = new Singleton();

   public static Singlegon getInstance()
   {
      return _instance;
   }
 }
KevinO
  • 4,303
  • 4
  • 27
  • 36