Question d’entretien chez Luxoft

Describe how you should write thread-safe singleton with lazy initialization.

Réponse à la question d'entretien

Utilisateur anonyme

13 mars 2021

public class Singleton { private static volatile Singleton instance; public static Singleton getInstance() { Singleton localInstance = instance; if (localInstance == null) { synchronized (Singleton.class) { localInstance = instance; if (localInstance == null) { instance = localInstance = new Singleton(); } } } return localInstance; } }

1