Application Object Pooling
Overview
Coding Pooled Objects
Sample Object
final public class Order implements Item<Order> {
final private static class OrderFactory implements UtlPool.Factory<Order> {
@Override
final public Order createItem(final Object context) {
return new Order();
}
@Override
final public Order[] createItemArray(final int size) {
return new Order[size];
}
}
// TODO: your member variables
private int orderQuantity;
// order pool - member variable to store pool this Order belongs to
private UtlPool<Order> pool;
/**
* Creates a new order pool with the provided number of preallocated orders.
*
* @param orderPreallocateCount The number of orders to preallocate in the pool
* @param poolName The name of the pool
* @return A new order pool
*/
public static UtlPool<Order> createPool(int orderPreallocateCount, String poolName) {
final UtlPool<Order> orderPool = UtlPool.create(
"order",
poolName,
new OrderFactory(),
UtlPool.Params.create()
.setThreaded(false)
.setInitialCapacity(orderPreallocateCount)
.setPreallocate(true)
);
return orderPool;
}
private Order() {
// initialization
init();
}
/**
* Implementation of {@link Item#init()}
*
* This method cleans a pool item when it is recycled to the pool or
* added for the first time.
*/
@Override
final public Order init() {
// TODO: reset your variables
orderQuantity = -1;
return this;
}
/**
* Implementation of {@link Item#setPool}
*
* Called by the pool to mark that this instance belongs to it.
*/
@Override
final public Order setPool(UtlPool<Order> pool) {
this.pool = pool;
return this;
}
/**
* Implementation of {@link Item#getPool}
*
* Gets the pool that this instance belongs to.
*/
@Override
final public UtlPool<Order> getPool() {
return pool;
}
}Implementing Reference Counting
Using Pools
Configuring Pools At Runtime
Pooling Configuration Properties
Property Name
Default
Description
See Also
Last updated

