Package generic.cache
Class CachingPool<T>
java.lang.Object
generic.cache.CachingPool<T>
- Type Parameters:
T
- the type of object to pool
A thread-safe pool that knows how to create instances as needed. When clients are done
with the pooled item they then call
release(Object)
, thus enabling them to be
re-used in the future.
Calling setCleanupTimeout(long)
with a non-negative value will start a timer when
release(Object)
is called to BasicFactory.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
ConstructorDescriptionCachingPool
(BasicFactory<T> factory) Creates a new pool that uses the given factory to create new items as needed -
Method Summary
Modifier and TypeMethodDescriptionvoid
dispose()
Triggers all pooled object to be disposed via this pool's factory.get()
Returns a cached or newT
void
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 Details
-
CachingPool
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 Details
-
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
Returns a cached or newT
- Returns:
- a cached or new
T
- Throws:
Exception
- if there is a problem instantiating a new instance
-
release
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.
-