I noticed an error in the Singleton pattern article by Rajesh V.S., dated 12/05/2001. I believe that the code to create the new instance should be synchronized using a 'lock'. I am coming from the Java world, where synchronizing the Singleton creation is common practice. I wrote a test program to try it out, and the instance count kept creeping up as I ran the program. I have included the source code below. Just alternately add and remove the lock in the Singleton.getInstance() method to see the effect of synchronization on the number of instances of Singleton that are created.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: