Vibur logo

vibur-object-pool

General-purpose concurrent Java object pool that is built entirely using standard Java concurrency utilities, does not use any synchronized blocks or methods, and does not have any external dependencies.

latest release  25.0

Changelog

Maven Coordinates

Vibur Object Pool is an excellent choice for pooling expensive-to-create Java objects, such as database socket connections and similar. Currently, it is used as a core building block of Vibur DBCP - a JDBC connection pool.

Concurrency and Performance Bounds

In terms of standard Java concurrency utilities, the object pool take and restore operations performance is bound by the performance of the ConcurrentLinkedQueue poll and add operations, where the CLQ is additionally guarded by a Semaphore in which the fairness parameter is set to true or false depending on the pool. While the pool implementation employs little additional logic for various house-keeping purposes, its overall performance remains a function of the mentioned above standard Java concurrency utilities.

Implementation Details

The pool provides support for fairness with regards to the waiting taker's threads via its fairness parameter. The creation of new objects in the pool and their lifecycle are controlled by the provided during the pool creation time PoolObjectFactory.

The ConcurrentPool does not provide any validation whether the currently restored object has been taken before that from the pool or whether the object is currently in taken state. Correct usage of the pool is established by programming convention in the application.

The pool also provides support for supplying a Listener interface parameter at its creation time. The supplied Listener object onTake and onRestore methods will be called upon calling the pool take and restore operations.

Another feature of the pool is the support for manual or automated shrinking (reduction) of the number of allocated in the pool objects, and the developer has full control over it via calling the reduceCreated pool methods or via using the SamplingPoolReducer utility class.

As of version 12.0 and 13.0 Vibur Object Pool added support for third-party collections having interface different than java.util.Queue/Deque to be used as an optional concurrent queue/collection by ConcurrentPool. Particularly, Vibur added support for some of the Conversant Disruptor collections. For example, now the ConcurrentPool can be configured like that:

PoolService<SomeClass> pool = new ConcurrentPool<>(
    new MultithreadConcurrentQueueCollection<>(100), new SomeClassFactory(), 1, 100, true);

Maven Dependencies

Vibur Artifact Coordinates and How to Build from Source

<dependency>
  <groupId>org.vibur</groupId>
  <artifactId>vibur-object-pool</artifactId>
  <version>25.0</version>
</dependency>

To get a local copy of the Vibur Object Pool repository use this command:

git clone https://github.com/vibur/vibur-object-pool.git

If needed, checkout a particular tag via something like:

git checkout tags/25.0

Building the sources is simply a matter of executing:

mvn clean install

Setting Up Object Pooling

Programming Example

PoolService<SomeClass> pool = new ConcurrentPool<>(
    new ConcurrentLinkedQueueCollection<>(), new SomeClassFactory(), 10, 60, false);

//...

SomeClass obj = null;
try {
    obj = pool.tryTake(100, TimeUnit.MILLISECONDS);
    if (obj == null) { // i.e., no objects were available in the pool within a duration of 100 milliseconds
        return;
    }

    // do some work that uses the obj here

} finally {
    if (obj != null) {
        pool.restore(obj);
    }
}       

More usage examples can be found in the project's unit tests folder.


Vibur Object Pool is designed and developed by Simeon Malchev, and is currently maintained by Simeon. If you need to report any issues, to request features, or if you just wish to provide some general feedback, please use the project's issues tracking system or the author's email.

The source code of all Vibur projects is distributed under Apache License 2.0, and is free to use for commercial or non-commercial use.