Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing a simple Object Pool  RSS feed

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard about Object Pooling and how it can improve application performance. Someone suggested using Commons Object Pooling API. But before that, i just wanted to get a feel of how actually an Object Pool could be implemented. So,( with a limited understanding of wait() and notify(), Singleton Pattern and Collections), i wrote something like ...



I want to have this code open for discussion; i am not sure if i used the right "endpoint"...please guide if you think i am lost.

Questions:
1. Is this correct implementation of Object pool?
2. is this a good design?
3. have i missed anything in my implementation: what are limitations of this implementation?
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object pooling is now a serious performance loss for all but the most heavyweight of objects, and even then it is tricky to get right without introducing concurrency bottlenecks


Java theory and practice: Urban performance legends, revisited
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoa!
That(article) is a nice reality check.
Thanks.

Assuming that we indeed will be pooling some real heavyweight Objects how does this simple pool stand?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monu:
Assuming that we indeed will be pooling some real heavyweight Objects how does this simple pool stand?


I am not very enthusiastic on providing the code to you but i can comment on your code (That is always easier)
  • I am not sure whether i would instantiate a common object pool for all the poolable objects. So, my implementation will be intended to instantiate an Object pool for a particular class(using generics).
  • Instead of having a map to store the "in use"/free status, i would like to have a list(stack/queue) that always have free objects and other that always have used objects. This will save me the map iteration to find a free object.
  • You are updating the objectUsageFlag map outside the synchronized blocks. Since both borrow and return methods update the map, there is always a possibility of a ConcurrentModificationException.


  • I am not sure how you intend to use this pool but it is better not to expose it directly to code that is instantiating the objects.
    It is better to hide it behind a factory, so that you can remove it whenever required.
    [ October 23, 2008: Message edited by: Nitesh Kant ]
     
    Monu Tripathi
    Rancher
    Posts: 1369
    1
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Nitesh,

    I understand the 1st and 2nd item in your list of recommendations; i am still unsure of what you meant my hiding the implementation behind a factory?
    Below are the changes i made:


    TIA...
     
    Nitesh Kant
    Bartender
    Posts: 1638
    IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Monu:
    i am still unsure of what you meant my hiding the implementation behind a factory?


    So, the code that wants to instantiate an object, must not directly call the ObjectPool but instead call a factory class that may internally use an ObjectPool or creates a new instance for every request.
    The advantage of this model is that you will have a control to stop/start using the pool if required by just changing the factory.
    In the other case, if you want to remove the usage of ObjectPool (for whatever reason) then you have to go ahead and modify a lot of classes that may be instantiating the objects.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!