File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton usage in Object Pool

 
saravanan kanda swamy
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saravanan kanda swamy
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic