This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton usage in Object Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton usage in Object Pool" Watch "Singleton usage in Object Pool" New topic
Author

Singleton usage in Object Pool

saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

I read in Thinking in Patters it mentioned that Singleton pattern is used in Object Pool.

I also saw the samples code in it. I couldn't figure the usage of singleton in object pool, the way i understood is that in Singleton pattern only one object will be created. This we do using private constructor. However I couldn't find private constructor in Object pool.

My doubt is if there is no private constructor then Object Pool is not based on Singleton pattern.

Another Reference in web link: http://sourcemaking.com/design_patterns/object_pool/java

Can somebody explain me why they say that Singleton is based on Object Pool.

Thanks in advance,
Saravanan.K
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That object-pool class in the example is abstract so making the constructor private would make the class unusable. Apparently the author expects that the class which extends it would enforce the singleton functionality.

I don't think I would use that class since Hashtable has kinda been replaced by ConcurrentHashMap and Iterator is favoured above Enumeration.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Thanks for reply.

I have made the changes, however I read that using iterator in ConcurrentHashMap is not advisable.

So I did the looping thorough with the entrySet.

I have attached the source code as well.



Will it be okay if I use entrySet instead of iterator?

More over I have doubt that using 2 ConcurrentHashMap will be thread safe as the logic is in such a away that if the validation is successful then it will be removed from the unlocked and same will be put in locked



Kindly clarify.

Thanks and Regards,
Saravanan K
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 196
Just to answer this question
Can somebody explain me why .. Singleton is based on Object Pool.


The singleton pattern provides you the ability to implement an object pool.
The object pool is, in this case, just a practical application of the singleton pattern.

There are, however, also several other applications of the singleton pattern.
Consider for example a cache to be implemented as a singleton.
Therefore the singleton pattern has several applications and the object pool is just one of them.

Hopefully this will clarify the question stated above.




saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Thanks for the reply.

However I have another doubt, Will it be okay if I use entrySet instead of iterator?

"And also using 2 ConcurrentHashMap will be thread safe as the logic is in such a away that if the validation is successful then it will be removed from the unlocked and same will be put in locked. "

Do we need to place them in synchronized block, as ConcurrentHashMap will be thread safe to itself not to each other.

Thanks and Regards,
Saravanan.K
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 196
Currently your question is related to the details of your own implementation of an Object Pool.
I understand that your interested in resolving the issue of your own implementation.

Object pooling is, however, a concept with several generic implementations.
I wonder if you've considered using one of the already available components that resolves your issues in a generic manner.
Just as an example. The Apache Commons project already provides a component called Object Pool for this purpose : http://commons.apache.org/pool/index.html
If you would like to sustain your own implementation and you have questions related to it.
You could have a look at the implementation of these components or start using them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton usage in Object Pool