Package com.esotericsoftware.kryo.util
Class Pool<T>
- java.lang.Object
-
- com.esotericsoftware.kryo.util.Pool<T>
-
public abstract class Pool<T> extends Object
A pool of objects that can be reused to avoid allocations. The pool is optionally thread safe and can be configured to use soft references.- Author:
- Nathan Sweet, Martin Grotzke
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Pool.Poolable
Objects implementing this interface will havePool.Poolable.reset()
called when passed tofree(Object)
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
clean()
If using soft references, all soft references whose objects have been garbage collected are removed from the pool.void
clear()
Removes all free objects from this pool.protected abstract T
create()
void
free(T object)
Puts the specified object in the pool, making it eligible to be returned byobtain()
.int
getFree()
The number of objects available to be obtained.int
getPeak()
The all-time highest number of free objects.T
obtain()
Returns an object from this pool.protected void
reset(T object)
Called when an object is freed to clear the state of the object for possible later reuse.void
resetPeak()
-
-
-
Constructor Detail
-
Pool
public Pool(boolean threadSafe, boolean softReferences)
Creates a pool with no maximum.
-
Pool
public Pool(boolean threadSafe, boolean softReferences, int maximumCapacity)
- Parameters:
maximumCapacity
- The maximum number of free objects to store in this pool. Objects are not created untilobtain()
is called and no free objects are available.
-
-
Method Detail
-
create
protected abstract T create()
-
obtain
public T obtain()
-
free
public void free(T object)
Puts the specified object in the pool, making it eligible to be returned byobtain()
. If the pool already contains the maximum number of free objects, the specified object is reset but not added to the pool.If using soft references and the pool contains the maximum number of free objects, the first soft reference whose object has been garbage collected is discarded to make room.
-
reset
protected void reset(T object)
Called when an object is freed to clear the state of the object for possible later reuse. The default implementation callsPool.Poolable.reset()
if the object isPool.Poolable
.
-
clear
public void clear()
Removes all free objects from this pool.
-
clean
public void clean()
If using soft references, all soft references whose objects have been garbage collected are removed from the pool. This can be useful to reduce the number of objects in the pool before callinggetFree()
or when the pool has no maximum capacity. It is not necessary to callclean()
before callingfree(Object)
, which will try to remove an empty reference if the maximum capacity has been reached.
-
getFree
public int getFree()
The number of objects available to be obtained.If using soft references, this number may include objects that have been garbage collected.
clean()
may be used first to remove empty soft references.
-
getPeak
public int getPeak()
The all-time highest number of free objects. This can help determine if a pool's maximum capacity is set appropriately. It can be reset any time withresetPeak()
.If using soft references, this number may include objects that have been garbage collected.
-
resetPeak
public void resetPeak()
-
-