This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Guys Please help, I am trying to figure out a problem in live with the following piece of code.
This is unusual coding style where in someone has created an Instance of an Singleton object using an local instance inside the method first and then finally assigning to static (Singleton) instance.
But what i find is Finalizer thread running frequently on these local instance which will destroy PinServer31 object which is not expected to happen by design
I understand that code should use static instance rather than local instance to sovle any problem. But i am not able to explain why currently the below code makes local instances to be garbage collected please can you explain.
The only way you'd see instances being garbage collected is if getInstance() is returning a new instance of the class each time. This would happen if the condition (instance._customerSet != customerSet) wasn't true for the static instance -- either never true, or true at first but then becoming untrue. Check out whether getInstance() ever returns a new instance when the old one exists by adding print or log statements or by running the code in a debugger.
Sam is right - the local variable doesn't serve any purpose.
The "instance._customerSet != customerSet" looks suspicious to me, too. Can you tell us more about what it might be intended to mean?
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
Joined: Jun 08, 2005
Hello Guys Thanks for your replies, I as well dont understand the reason why the code was written like this. This is a existing piece of code in live which is causing trouble as I can see finalizer thread being run ever minute to clean up something which destroyes PinServer31 causing huge performance implications which I have found.
I am sure there is something wrong and as well I understand that the way to fix is to remove the local instance from the getInstance method.
What I dont understand is why is the local instance garbage collected every minute when there is a reference to it by the static instance?
I need to explain the problem as to why this code is incorrect, to fix it.
Please help! how will you reason out why it is incorrect and why does the local reference to be garbage collected often?
Thanks Mohamed Farouk
Joined: Jun 08, 2005
Hello Please do not worry about customer set , as i forgot to remove that line before posting it. The code in live contains that attribute as well but i thaught i can remove it for clarity. So please do not worry about it.