samedi 4 juillet 2015

Is this Singleton class in Java acceptable?

I used the concepts from a published tutorial on Singleton creation but would like some feedback. Is there a preferred method?

public class MyDoubleLockSingleton {

    private volatile static Object _mySingleton;

//private constructor
    private MyDoubleLockSingleton() {
        if (_mySingleton == null) {
            MyDoubleLockSingleton.getSingleton();
        }
    }

//  double lock singleton approach
    public static Object getSingleton() {
        synchronized (MyDoubleLockSingleton.class) {
            if (_mySingleton == null) {
            _mySingleton = new Object();
        }
        return (Object)_mySingleton;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire