E - public class ObjectPool<E>
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
protected E[] |
arrayPool
The low-level array holding the object instances.
|
protected int |
capacity
Capacity of the pool, i.e max number of Objects managed by this pool.
|
protected int |
currentSize
Current number of available (free) objects currently in the pool.
|
protected ObjectFactory<E> |
factory
The
ObjectFactory used for creating or re-initializing objects
of the pool. |
protected ArraySizingStrategy |
growthPolicy
Controls the way more objects are produced when the pool is empty.
|
| Constructor and Description |
|---|
ObjectPool(ObjectFactory<E> objFactory,
int initialSize,
ArraySizingStrategy growthPolicy)
Create a new pool of Objects E, constructed with objFactory, of size initialSize
at startup, with growthStrategy as a policy when new objects need to be allocated.
|
| Modifier and Type | Method and Description |
|---|---|
E |
borrow()
Borrow an Object from the pool.
|
int |
capacity()
get the capacity of the pool, i.e max number of Objects managed by this pool.
|
void |
release(E releasedObject)
Release this object E by putting it back to the pool.
|
int |
size()
get the current number of available (free) objects in the pool.
|
protected ObjectFactory<E> factory
ObjectFactory used for creating or re-initializing objects
of the pool.protected int currentSize
protected int capacity
protected ArraySizingStrategy growthPolicy
ArraySizingStrategy.grow() differs a bit in the
ObjectPool context: a) if grow() >= 0, follow the original contract,
b) if -capacity() < grow() < 0 (grow() == -nbNewElements), it means that the pool is to generate nbNewElements Objects, but the capacity is unchanged, which means that (nbNewElements) Objects allocated previously are virtually discarded, for GC to collect. This particular policy prevent the pool to grow indefinitely if some reference Objects get "lost" over time when a proper release() was impossible to do on them.
release(E)protected E[] arrayPool
public ObjectPool(ObjectFactory<E> objFactory, int initialSize, ArraySizingStrategy growthPolicy)
objFactory - initialSize - growthPolicy - public E borrow()
growthPolicy
policy. The returned object is assured to be initialized with factory.initialize().public void release(E releasedObject)
releasedObject - public int size()
public int capacity()
Copyright © 2017. All rights reserved.