Sunday, September 7, 2014

Double Locking Or Double check in Singleton Class

Double Locking Or Double Check in Singleton


Now these days it is a frequently asked question that how you will create double locking singleton class.

It is nothing new as earlier we were using Synchronize key-work before method, the same key world we will use while creating instance of the singleton class.

See the below written code snap.


private SingletonInstance instance=null;

 public SingletonInstance getInstance() {
        if (instance == null) {
            synchronized (SingletonInstance.class) {
                if (instance == null) {
                    instance = new SingletonInstance();
                }
            }
        }
        return instance;
    }

1 comment:

Spring Boot Config Server and Config Client.

 In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...