Grokking Java Interview Questions
0% completed
Implement a Thread Safe Singleton
Two classes each promise to create one price service. The first one writes static final PriceService INSTANCE = new PriceService();, and it keeps the promise with any number of threads. The second one waits until the first call, tests if (instance == null), and creates the object there. It keeps the promise with one thread, and it can break the promise with two.
A class that creates one object of itself and gives that object to all callers is called a singleton. The sections below start from the version that fails and build five designs
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%