About Me

My photo
Bangalore, Karnataka, India

Wednesday, June 16, 2010

DCL and Synchronization

Myself being a C++ developer before, was writing code with Double checked lock pattern untill I realized that doing so is a crime in Java. The reason goes like this, the JVM implementor and the compiler are free to reorder the code in the non-synchronized block and we may endup getting the stale object reference in many a cases. But in few implementations is works fine as read over google. So the thumb rule is no DCL in JAVA. There were few links which tells there is some JSR coming in the way to solve this (JMM specs for this).

Volatile is the solution for this.