2

I was asked in an interview to suggest a design/implementation of a Singleton Pattern where I have to Lazy load the class and also not use the synchronized key word. I got choked and could not come up with anything.I then I started reading on java concurrency and concurrentHaspMap. Please check the below imlpementation and kindly confirm if you see any issue with Double check Locking or any other issues with this implementation.

package Singleton;

import java.util.concurrent.ConcurrentHashMap;

public final class SingletonMap {

    static String key = "SingletonMap";
    static ConcurrentHashMap<String, SingletonMap> singletonMap = new ConcurrentHashMap<String, SingletonMap>();
    //private constructor
    private SingletonMap(){

    }
    static SingletonMap getInstance(){

        SingletonMap map = singletonMap.get(key);       
        if (map == null){
                //SingletonMap newValue=  new SingletonMap();
                map =   singletonMap.putIfAbsent(key,new SingletonMap());
                if(map == null){
                    map = singletonMap.get(key);    
                }
        }       
        return map;
    }
}
2
  • I think that they wanted to hear something about atomicity. Commented Aug 9, 2012 at 7:54
  • This implementation is unnecessarily convoluted. There are simpler ways to create a singleton - See the answers posted by Peter and me below. Commented Aug 9, 2012 at 8:35

2 Answers 2

3

Its pretty simple if you know how

enum Singleton {
    INSTANCE;
}

The INSTANCE is lazy loaded and thread safe (and doesn't use explict locking of any kind)

Sign up to request clarification or add additional context in comments.

2 Comments

sorry but I do not know much about enums yet and I think you may be right too but can you comment on the implementation I had shared? Do see any loopholes in it?
It could create more than one SingletonMap and it is far more complicated than it needs to be. Otherwise it could work.
2

See also Bill Pugh's solution

public class Singleton {
    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    /**
     * SingletonHolder is loaded on the first execution of
     * Singleton.getInstance() or the first access to
     * SingletonHolder.INSTANCE, not before.
     */
    private static class SingletonHolder {
        public static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

2 Comments

Just want to aks you how can this be any different if I use "public static final Singleton INSTANCE = new Singleton(); " as part of Singleton class member variable? what benefit does Inner class provides ?
The static inner class is only created when either getInstance() is called or INSTANCE is accessed. A static instance variable will be created the moment it's parent class is loaded by the Classloader. See the Java Language Specification for details on how static instance varaibles are initialised.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.