The use of the design pattern "Singleton" is very common and the common implementation is using a static reference. However, according to Guy Korland, the problem with this implementation is that many times it leaves "floating garbage that is not in use." As a better solution, Joshua ben Jore has introduced WeakSingleton. This extention cleans the instance when all other references to the original instance have expired.
- Posted by: Regina Lynch
- Posted on: July 28 2006 11:25 EDT
Singleton is a commonly (mis) used design pattern. Users of this pattern should be aware of the scope & lifecycle control of the singleton object. Singleton could be at various scope levels. 1. Singleton object within a operating system environment or JVM. 2. Singleton object across clustered environments. It may not be considered singleton from application's perspective. 3. Singleton in a multi-threaded application. It should be thread safe for which programming Idioms are available.
The code given is not threadsafe and using volatile with 1.5 may not solve the problem.
Why is the above code not threadsafe? why do u think using volatile with 1.5 not solve problems?
do u think a local variable should be used to create an instance and assign this vraiable to the other private variable?