The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
There is no emoticon for what I am feeling!
Originally posted by B Sathish:
I think I understand now why that variable needs to be volatile. [...] You need them to be volatile only when you use them outside of the synchronized block.
Originally posted by B Sathish:
By the way, Paul, that implementation that you would prefer to use is simple, but not reliable. It can violate the singleton pattern by creating 2 instances if 2 threads creep into the getInstance method at the same time. Hence you need to synchronize the method.
There is no emoticon for what I am feeling!
There is no emoticon for what I am feeling!
Originally posted by B Sathish:
The version that you have given is an example of an eagerly-created singleton instance. While it would be simple and thread-safe for most situations, it has the draw-back that it is resource-intensive as the instance is eagerly created. What if the Singleton instance is a highly resource-intensive object? and what if you cannot say for sure when the singleton instance would be used? and what if there is a chance that it would not be used at all? pre-loading this instance in memory would be a waste of resource right? That is when the classic implementation makes sense as it creates the instance lazily only when needed, but it is not thread-safe.
There is no emoticon for what I am feeling!
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|