Package generic.cache
Class CachingPool<T>
- java.lang.Object
-
- generic.cache.CachingPool<T>
-
- Type Parameters:
T
- the type of object to pool
public class CachingPool<T> extends java.lang.Object
A thread-safe pool that knows how to create instances as needed. When clients are done with the pooled item they then callrelease(Object)
, thus enabling them to be re-used in the future.Calling
setCleanupTimeout(long)
with a non-negative value will start a timer whenrelease(Object)
is called toBasicFactory.dispose(Object)
any objects in the pool. By default, the cleanup timer does not run.Once
dispose()
has been called on this class, items created or released will no longer be pooled.
-
-
Constructor Summary
Constructors Constructor Description CachingPool(BasicFactory<T> factory)
Creates a new pool that uses the given factory to create new items as needed
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
dispose()
Triggers all pooled object to be disposed via this pool's factory.T
get()
Returns a cached or newT
void
release(T t)
Signals that the given object is no longer being used.void
setCleanupTimeout(long timeout)
Sets the time to wait for released items to be disposed by this pool by callingBasicFactory.dispose(Object)
.
-
-
-
Constructor Detail
-
CachingPool
public CachingPool(BasicFactory<T> factory)
Creates a new pool that uses the given factory to create new items as needed- Parameters:
factory
- the factory used to create new items
-
-
Method Detail
-
setCleanupTimeout
public void setCleanupTimeout(long timeout)
Sets the time to wait for released items to be disposed by this pool by callingBasicFactory.dispose(Object)
. A negative timeout value signals to disable the cleanup task.When clients call
get()
, the timer will not be running. It will be restarted again oncerelease(Object)
has been called.- Parameters:
timeout
- the new timeout.
-
get
public T get() throws java.lang.Exception
Returns a cached or newT
- Returns:
- a cached or new
T
- Throws:
java.lang.Exception
- if there is a problem instantiating a new instance
-
release
public void release(T t)
Signals that the given object is no longer being used. The object will be placed back into the pool until it is disposed via the cleanup timer, if it is running.- Parameters:
t
- the item to release
-
dispose
public void dispose()
Triggers all pooled object to be disposed via this pool's factory. Future calls toget()
will still create new objects, but the internal cache will no longer be used.
-
-