Is this implementation of a thread-safe singleton correct? And how can it be implemented without synchronized and volatile?
private static Singleton instance; private static Lock lock = new ReentrantLock(); private static AtomicBoolean flag = new AtomicBoolean(); public static TaxiCompany getInstance() { if (!flag.get()) { lock.lock(); try { if (instance == null) { instance = new TaxiCompany(); flag.set(true); } } finally { lock.unlock(); } } return instance; }