|
Re: Concurrent Singleton usage - Limitations ?
|
Posted: Jul 3, 2004 11:35 AM
|
|
Shashank First let me correct my post: I missed static keywords in both places in variable declaration and getInstance() method.
You have started discussing something that was not mentioned in the original post at all. The question was: Is there any limitations on number of callers as far as getInstance() method is involved. That's all the question was. The answer is "No". The overhead of calling getInstance() method can be removed by not synchronizing it and creating the object instance while declaring the static field. That's all the answer should be. I don't understand why you would start explaining about apple if someone asked explanation on mango. I understand other guy who replied to your post. He is just like that. Most of the time he talks about Python and Gython when someone asks question about Java. I have seen many good posts from you. My advise to you that just ignore that guy who just wants to make fun of others.
I will answer your questions which you have raised in your posts:
--But if you don synchronize it, it is possible to create multiple instances of object at some point of time. Then why singleton pattern?
Answer: There will be only one instance all the time no matter how many users are calling it simultaneously. Because, the instance is created only once when class is loaded for the first time.
--Though you may not need to declare the method as public static synchronized. You may use more optimized menchanism, but point is still some processing is to be done?? and for what? just to get the access the object, and if you are handling or upating some state information, the complexity involved in simlutaneious execution trying to update the state, is high.
Answer: Since the original question doesn't mention anything about the state of its object - heavy/light, all these explanations are irrelevant in this context.
--This approach will work fine, if the Singleton Class doesnt have heavy states.
Also by creating the instance before being sure of when and whether it will be used is not better design.
Answer: With the approach I have mentioned and with the Singlton Pattern, the instance will be created when it is needed for the first time. Since instance is created when class is loaded, it will be created when yo call its getInstance() method first time. Here is a catch: If the class developer doesn't use the proper semantics of having a Singlton pattern then he may have some static methods other than getInstance() and the call to that methods will also create the object. However, that will happen only when someone is trying to use Singlton pattern in a wron way.
-- Again, assuming everything is fine, what about, the methods that will be called, are they synchronized properly?
Answer: Again, you are speculating about its use. Only the developer who posted the question originally can answer this question if that is the case. And, if he has to have a shared resource, which can be modified by many users and must be synchronized then it must be synchronized. The optimization techniques depends on the situation at hand.
--Making a thread safe singleton class with lots of states is little bit complex. Thats all.
Many only intention is to say that be open for other options to explore instead of having an "I have to use singleton class" attituted.
Answer: Again, I will say that I don't want to comment on somethng about which I don't know. Just look at your comment: "Making a thread safe singleton class with lots of states is little bit complex." Does it serve any purpose to anybody who reads it. Your statement sounds like a politian statement.
On a forum, it is essential that you keep your answers focussed on the original question. If you want to give some extra information then make it brief and complete. Don't answer a question just because you want to answer it. Developers from around the globe will be spending time to read it. The very purpose of your taking time and answering the question is defeated if it doesn't help someone.
|
|