zinger.util.recycling
Interface ObjectPool

All Known Implementing Classes:
CappedObjectRecycler, ObjectRecycler, SynchronizedObjectPool

public interface ObjectPool

Allows for reuse of instances without having to constantly instantiate new ones and discard old ones. Whenever there is a high turn-over rate of certain type of objects, relying on instantiation and garbage collection can significantly slow down the system. Instantiation is a fairly expensive process and garbage collection usually doesn't happen until it has to, which means memory gets eaten away at until the last moment when everything slows down to let GC to kick in. ObjectPool works by simply caching instances. Instantiation and preparation of instances is delegated to an implementation of ObjectGenerator.

Historically, before this interface was added, users had to explicitly refer to ObjectRecycler explicitly. A better practice now is to only type your references as ObjectPool.

See Also:
ObjectRecycler

Method Summary
 void clearCache()
          Clears existing cache.
 java.lang.Object getObject()
          Gets an instance out of the pool with default initialization.
 java.lang.Object getObject(java.lang.Object arg)
          Gets an instance out of the pool and initializes it using the specified parameter object.
 boolean recycleObject(java.lang.Object obj)
          Returns an object into the pool.
 

Method Detail

getObject

java.lang.Object getObject()
                           throws java.lang.IllegalArgumentException
Gets an instance out of the pool with default initialization. This is equivalent to calling getObject(null).

Throws:
java.lang.IllegalArgumentException
See Also:
getObject(java.lang.Object)

getObject

java.lang.Object getObject(java.lang.Object arg)
                           throws java.lang.IllegalArgumentException
Gets an instance out of the pool and initializes it using the specified parameter object.

Parameters:
arg - Initialization parameter. If null, default initialziation is used.
Throws:
java.lang.IllegalArgumentException

recycleObject

boolean recycleObject(java.lang.Object obj)
Returns an object into the pool. By calling this method, the user guarantees not to perform any more operation on the passed-in instance. The pool does not guarantee the passed-in instance will be accepted, or if accepted that it will ever be returned by getObject() or getObject(arg).

See Also:
getObject(), getObject(java.lang.Object)

clearCache

void clearCache()
Clears existing cache. This could be useful if a memory monitor was connected to this instance and ran in the background. It could know to discard cache when activity is down and memory usage is high.