|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lava.util.ObjectRecycler
Maintains a pool of recyclable objects.
Details: ObjectRecycler
provides a mechanism for avoiding the overhead of perpetual object construction and destruction, in programs that otherwise would repetitively instantiate objects of a certain class, use them momentarily, and then discard them. ObjectRecycler
allows retired objects to be manually returned to a pool for later reuse.
Each ObjectRecycler
is permanently associated with a Class
. Calls to construct
return either a new instance of the class, or an old instance that has been used and returned to the pool. When new instances are constructed, the default constructor is used. New instances are constructed only when the pool is empty. When old instances are reused, no initialization is performed on them; they are returned in the same condition they were in when they were recycled. If additional initialization requirements exist, the developer should implement the necessary behavior in a subclass of ObjectRecycler
.
class StringBufferRecycler extends ObjectRecycler { StringBufferRecycler () { super (new StringBuffer () . getClass (), 10); } StringBuffer getBuffer () { StringBuffer buf = (StringBuffer) construct (); buf . setLength (0); return buf; } }
Field Summary | |
protected java.lang.Class |
cls
The class of objects maintained by this pool. |
protected int |
max_size
Maximum size of pool. |
protected java.util.Stack |
pool
The pool. |
Constructor Summary | |
ObjectRecycler(java.lang.Class cls,
int max_size)
Indicates the cls of this pool. |
Method Summary | |
java.lang.Object |
construct()
Returns an instance of cls . |
void |
recycle(java.lang.Object o)
Returns an instance to the pool. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected final java.lang.Class cls
Details: All objects handled by this ObjectRecycler are immediate instances of class
.
protected final java.util.Stack pool
Details: The pool is maintained as a Stack so that instances most recently recycled will be immediately reused. This will increase object locality and, hopefully, allow for better cache performance.
protected final int max_size
Details: max_size
is the maximum number of objects that can be stored in the pool. When the pool is full and additional objects are recycled, they are not entered into the pool. (Presumably, these rejected objects will be garbage collected.)
Constructor Detail |
public ObjectRecycler(java.lang.Class cls, int max_size)
Details: This constructor binds this ObjectRecycler
to the given class. All objects that enter and leave the pool will be direct instances of cls
. (Hence, cls
must not be abstract.)
max_size
- maximum pool sizecls
- the pool clsMethod Detail |
public java.lang.Object construct()
cls
.
Details: construct
returns an instance of cls
from the pool, or constructs and returns a new one if the pool is empty, using the default constructor for cls
.
If this method is called but there is no accessible default constructor for cls
, an UnreachableCodeException
will be thrown. This indicates programmer error.
public void recycle(java.lang.Object o)
Details: recycle
enters the given object into the pool, for later reuse, with the assumption that no other threads or objects will be using it anymore. If this assumption is wrong, the results may be disasterous.
o
- the instance to recycle
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |