Friday, September 09, 2005

Double checked Locking idiom and the Singleton pattern.

I have seen a lot of programs using the double-checked locking pattern in their singleton classes to avoid the overhead of synchronization for each method call.

But it is now known that the double-check locking idiom does not work. It is bcoz of the memory model of Java, which allows out-of-order writes to memory. i.e. There exists a window of time, when an instance is not null, but still not fully initiliazed(constructor has not returned) !!!

For more explanation, check out this simple and lucid article:

No comments:

Post a Comment