DEV Community

Discussion on: Ways to create Singletons and the tradeoffs between them

cicirello profile image
Vincent A. Cicirello • Edited on

That article on double checked locking is extremely out-dated. It was written pre-Java 4. See the phrase "expected ... in 1.4." However, enough was known at the time of the then-upcoming JDK 5 that the authors also explain near the end the very simple way of getting double checked locking to work beginning with JDK 5. You need to declare the field as volatile.

So to fix your double checked locking example just declare instance as volatile.

Thread Thread
abh1navv profile image
Abhinav Pandey Author