E
- is the generic type of the pooled elements.public abstract class AbstractPool<E> extends Object implements Pool<E>
Pool
interface.Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CAPACITY
The default capacity used.
|
private Object |
lock
The synchronization lock.
|
private Object[] |
pool
The buffer representing the pool.
|
private int |
size
The actual size (number of elements in
pool ). |
Modifier | Constructor and Description |
---|---|
|
AbstractPool()
The constructor.
|
private |
AbstractPool(boolean threadsafe,
int capacity,
Object lock)
The constructor.
|
|
AbstractPool(int capacity,
boolean threadsafe)
The constructor.
|
|
AbstractPool(int capacity,
Object lock)
The constructor for a thread-safe pool with an externally given
lock for synchronization. |
Modifier and Type | Method and Description |
---|---|
E |
borrow()
This method borrows an element from this pool.
|
private E |
borrowInternal() |
void |
clear()
This method clears the complete pool.
|
private void |
clearInternal() |
protected abstract E |
create()
This method creates a new element.
|
int |
getCapacity()
This method gets the capacity of this pool.
|
int |
getSize()
This method gets the size of the pool.
|
boolean |
isEmpty()
This method determines if the pool is empty.
|
void |
release(E element)
This method releases the given
element . |
protected boolean |
reset(E element)
This method resets the given
element so it can be reused. |
public static final int DEFAULT_CAPACITY
private final Object[] pool
private final Object lock
private int size
pool
).public AbstractPool()
public AbstractPool(int capacity, boolean threadsafe)
public AbstractPool(int capacity, Object lock)
lock
for synchronization.capacity
- is the capacity
.lock
- is an object used for synchronization.private AbstractPool(boolean threadsafe, int capacity, Object lock)
threadsafe
- if true
the pool needs to be accessed by multiple Thread
s concurrently,
false
otherwise.capacity
- the capacity of this pool.lock
- is an explicit synchronization-lock object. It may be null
. If it is NOT null
,
threadsafe
has to be true
.protected abstract E create()
protected boolean reset(E element)
element
so it can be reused. It is called if an element is
released
and will be stored in the pool. The implementation depends on the type of
element. Some types may become inconsistent if they are directly reused. Further this method may clear data from
the element for security reasons, because the same instance may be given to some other component that is NOT
trusted enough (because it may be vulnerably).element
- the element to reset.true
if the given element
can be reused and should be added to the pool, false
otherwise.public boolean isEmpty()
Pool
isEmpty
in interface Pool<E>
true
if the pool is empty.Collection.isEmpty()
public int getSize()
public int getCapacity()
size
of this pool. If that
size is reached further released
objects are NOT pooled and can be freed by the
garbage-collector. Pool
depends heavily on the type of objects that are pooled. For some lightweight
objects a very high capacity may suite. However for big buffers (e.g. char[65536]
) the capacity should be
very low because otherwise your heap might get crowded.public void release(E element)
Pool
element
. It will be put back into the pool. element
is NOT in use anymore. Therefore no reference
should exist on the element
and you should NOT have passed the element
to a third-party library
that may keep it in some cache.release
in interface Pool<E>
element
- is the element to add to the pool.Collection.add(Object)
public void clear()
empty
.private void clearInternal()
clear()
Copyright © 2001–2016 mmm-Team. All rights reserved.