Wednesday, September 28, 2005 02:46 AM
- Yes, it does not implement the singleton pattern correctly.
- ... and thus it is not a singleton.
- The name of the method doesn't matter, but it must be
The problem with the above implementation is, that
instance_already_created is not synchronized - a thread could observe it in
false state, proceed to the if-block, and then, another thread could observe it before it has been changed to
true. You could possibly fix it by switching to a
Boolean object and synchronize the contents of the constructor on it, but on the other hand, why not stick with the tried and true
static synchronized getInstance()?