mardi 26 novembre 2019

Is this singleton implementation correct?

As the double lock checking doesnt works on optimized compilers I am removing it from my singleton class and instead going for early initialization.

Below is my new singleton class :

class MySingleton {

private static volatile MySingleton myInstance = new MySingleton();

public static getInstance() {

    return myInstance ;
}

}

Apart from getInstance() there are setter methods in the class which set the values of the member fields.

Hope this implementation will not cause any data inconsistency when multiple threads will be updating various member fields using the same object.

Any suggestions or inputs are welcome.

Aucun commentaire:

Enregistrer un commentaire