June 2018
Intermediate to advanced
280 pages
7h 46m
English
One of the best implementations of the singleton pattern in Java relies on the fact that a class is loaded a single time. By instantiating the static member directly when declared, we make sure that we have a single instance of the class. This implementation avoids locking mechanisms and additional checking to see whether the instance has already been created:
public class LockFreeSingleton{ private static final LockFreeSingleton instance = new LockFreeSingleton(); private LockFreeSingleton() { System.out.println("Singleton is Instantiated."); } public static synchronized LockFreeSingleton getInstance() { return instance; } public void doSomething() { System.out.println("Something is Done."); }}
Read now
Unlock full access