Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find a modern Implementation of Object Pool in Java [closed]

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!

like image 685
time4tea Avatar asked Sep 03 '10 13:09

time4tea


People also ask

What is object pooling in Java with example?

An object pool is a collection of a particular object that an application will create and keep on hand for those situations where creating each instance is expensive. A good example would be a database connection or a worker thread. The pool checks instances in and out for users like books out of a library.

What is pooling in Java?

Connection pooling is a technique of creating and managing a pool of connections that are ready for use by any thread that needs them. Connection pooling can greatly increase the performance of your Java application, while reducing overall resource usage.

What OO design pattern is an object pool?

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object.


2 Answers

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.
like image 191
Pascal Thivent Avatar answered Oct 08 '22 02:10

Pascal Thivent


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.
like image 22
emory Avatar answered Oct 08 '22 01:10

emory