What is the problem with double-checked locking for singletons before Java 5, and how did Java 5 fix it?

Java Professional Hard

Java Professional — Hard

What is the problem with double-checked locking for singletons before Java 5, and how did Java 5 fix it?

Key points

  • Java 5 introduced volatile to prevent reordering of writes in double-checked locking
  • This ensures that a fully constructed object is visible to all threads
  • The volatile keyword establishes a happens-before relationship, ensuring correct object initialization

Ready to go further?

Related questions