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 Object
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 Details

    • 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 Details

    • setCleanupTimeout

      public void setCleanupTimeout(long timeout)
      Sets the time to wait for released items to be disposed by this pool by calling BasicFactory.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 once release(Object) has been called.

      Parameters:
      timeout - the new timeout.
    • get

      public T get() throws Exception
      Returns a cached or new T
      Returns:
      a cached or new T
      Throws:
      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 to get() will still create new objects, but the internal cache will no longer be used.