Domanda di colloquio di Luxoft

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

Risposta di colloquio

Anonimo

13 mar 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