T
- the class of objects stored in the instance poolabstract class FixedInstancePool<T>
extends java.lang.Object
This is essentially an immutable circular queue. Elements are not added nor removed. Pointers to the head and tail of the queue identify what is the next available entry.
Use allocate()
to get an available Entry
from the pool. If all objects are allocated then the thread will block until an element is released.
release(Entry)
releases an allocated Entry
for reuse.
Entry
Modifier and Type | Field and Description |
---|---|
(package private) int |
count |
private int |
head |
private int[] |
instanceIndexes |
(package private) Entry<T>[] |
instancePool |
private int |
lastInstanceIndex |
private int |
tail |
Constructor and Description |
---|
FixedInstancePool(int size)
Creates a new instance pool with the given size.
|
Modifier and Type | Method and Description |
---|---|
Entry<T> |
allocate()
Retrieves the next available entry in this instance pool.
|
protected abstract T |
newInstance()
Creates a new instance of the given type of objects stored as entries of this instance pool
This method is called in the constructor of this class for initialization of the instance array and must always return a new instance.
|
void |
release(Entry<T> e)
Releases the given entry and makes it available for allocation (by
allocate() ) |
private final int[] instanceIndexes
private int head
private int tail
int count
private int lastInstanceIndex
FixedInstancePool(int size)
newInstance()
method will be called to fill in the instance pool, and the pool
can then have its entries allocated for use (and reuse).size
- the size of the fixed instance pool.protected abstract T newInstance()
public Entry<T> allocate()
release(Entry)
).public void release(Entry<T> e)
allocate()
)e
- the entry to be released and made available for reuse.