views:

364

answers:

6

I'm looking for a modern implementation of an object pool in Java. I can see the apache commons one, but to be honest, I'd rather one that uses generics, and the concurrency stuff from more recent versions of java.

Does the commons pool really work well? The code looks pretty, erm, ugly.

I'd need something that allows custom liveness validation etc etc.

Thanks!

+7  A: 

I can see the apache commons one, but to be honest, I'd rather one that uses generics, and the concurrency stuff from more recent versions of java.

Well, the fact is that this kind of projects (generic object pools) don't get much traction because there is little need for them nowadays (object creation is cheap). This probably explains why you don't see much of them (and actually, I'm only aware of Commons Pool).

That being said, if generics is your primary concern, you could patch Commons Pool, see POOL-83, it has a patch attached.

Does the commons pool really work well? The code looks pretty, erm, ugly.

It does have a few known bugs (four) but, to my knowledge, it works. And regarding the last sentence, well, if you think you can write something better, and if you have the time for that, why not just doing it?

I'd need something that allows custom liveness validation etc etc.

You don't have an infinite number of options. Either

  1. Find something that does everything you need (I don't know such a library, which doesn't mean there isn't any).
  2. If you can't find something that does everything you need out of the box, then extend an existing solution.
  3. Roll your own solution.
Pascal Thivent
Thanks for your answer - I'm fully aware that object creation, per se, is very cheap. I would never use an object pool to reuse object that exist only in memory. There be dragons. However, often objects represent some external expensive-to-create resource that you cannot create cheaply. Think database connection, or SSL connection. Its this type of think that I'd like to pool. I have updated the commons pool code. I'll post a patch here or something.
time4tea
@time4tea I was just trying to anchor my answer, not implying that you weren't aware. But thanks for the feedback and making the patch available.
Pascal Thivent
A: 

To pool is the traditional way, to cache is the modern way. And there are many modern implementation of cache out there.

To learn the different between those two, you can read this: http://www.informit.com/guides/content.aspx?g=java&seqNum=104

My view is, we can use a cache library to pool our objects but not the other way around. Just don't forget to re-initialize the object after we get it from the cache. So why bother having two different animals (cache and pool) if you can achieve all just using one?

nanda
thanks. almost no information there.
time4tea
Maybe he's talking about distributed caching. Something like memcached? I guess that's a 'pool' of resources, in a way.
jamiebarrow
I've updated my answer...
nanda
+2  A: 

This seems to be related to your question, maybe you should really consider to write a object pool by your own. Does this basic Java object pool work?.

Pooling was initially introduced as a tuning action for the slow performance of object creation and garbage collection in particular. On a modern JVM > 1.4 pooling is no more needed for the optimization of memory management in a typical business application. It can even have a negative effect on the garbage collector performance. In special cases, like creating millions of instances in every method call, it could still pay off.

Instance pooling, however is still interesting for objects with slow custom "post construction". In some cases you want to inject some dependencies after the object creation, read some configuration etc. This can be slow and doesn't have to be performed over and over again. In such cases object pooling will improve the overall performance.

Adam Bien -- Object Pooling Can Be Still Useful - For Entirely Different Reasons

What do you think of enhancing the commons Pool Framework? You could do some refactoring and add the generic part, would be nice for others too.

codedevour
+1  A: 

Commons Pool is a good candidate for your project.

  1. Generics Interface - The most obvious problem with commons pool is its pre-generics interface. There are a number of ways you can get around this. You can
    1. do casting;
    2. implement a parallel interface that does the casting for you; or
    3. use the patch that Pascal identified
  2. Concurrency Stuff from more recent java - This is an implementation detail you should not care about. If the concurrency is correct, then it does not matter how correctness was achieved. Alternatively, a pool implementation that uses the more recent stuff but whose concurrency is wrong is still a poor candidate.
  3. Ugly Code - You are supposed to use it, not marry it.
  4. Custom Liveness Validation - Implement the validateObject to test the liveness of objects. Dead objects will be destroyed. You can also implement a Cron task to periodically borrow and return objects - forcing the timely elimination of dead objects.
emory
A: 

It's hard to make a recommendation without knowing what features you need.

If the number of objects in the pool is fixed, you can use a BlockingQueue as in this example from the question mentioned by @codedevour

If the values you want to pool can be associated with a key, you can use MapMaker from Guava

ConcurrentMap<Key, Connection> connections = new MapMaker()
       .concurrencyLevel(32)
       .softKeys()
       .weakValues()
       .expiration(30, TimeUnit.MINUTES)
       .evictionListener(
           new MapEvictionListener<Key, Connection>() {
             public onEviction(Key key, Connection connection) {
               connection.close();
             } 
           });
       .makeComputingMap(
           new Function<Key, Connection>() {
             public Connection apply(Key key) {
               return createConnection(key);
             }
           });
NamshubWriter
i love this example of MapMaker. It doesn't do what i need this time though!
time4tea
What features do you need from the object pool? What is it used for?
NamshubWriter
A: 

For the generics side, why not just use the non-generic library, and create a wrapper that you use to access the non-generic library that takes care of the casting? That way there's a single place where the casting is done, which will at least clean up the code a bit.

jamiebarrow