Class PoolUtils


  • public final class PoolUtils
    extends Object
    This class consists exclusively of static methods that operate on or return ObjectPool or KeyedObjectPool related interfaces.
    Since:
    Pool 1.3
    Version:
    $Revision: 1222670 $ $Date: 2011-12-23 08:18:25 -0500 (Fri, 23 Dec 2011) $
    Author:
    Sandy McArthur
    • Constructor Detail

      • PoolUtils

        public PoolUtils()
        PoolUtils instances should NOT be constructed in standard programming. Instead, the class should be used procedurally: PoolUtils.adapt(aPool);. This constructor is public to permit tools that require a JavaBean instance to operate.
    • Method Detail

      • checkRethrow

        public static void checkRethrow​(Throwable t)
        Should the supplied Throwable be re-thrown (eg if it is an instance of one of the Throwables that should never be swallowed). Used by the pool error handling for operations that throw exceptions that normally need to be ignored.
        Parameters:
        t - The Throwable to check
        Throws:
        ThreadDeath - if that is passed in
        VirtualMachineError - if that is passed in
        Since:
        Pool 1.5.5
      • checkedPool

        public static <T> ObjectPool<T> checkedPool​(ObjectPool<T> pool,
                                                    Class<T> type)
        Wraps an ObjectPool and dynamically checks the type of objects borrowed and returned to the pool. If an object is passed to the pool that isn't of type type a ClassCastException will be thrown.
        Type Parameters:
        T - the type of object
        Parameters:
        pool - the pool to enforce type safety on
        type - the class type to enforce.
        Returns:
        an ObjectPool that will only allow objects of type
        Since:
        Pool 1.3
      • checkedPool

        public static <K,​V> KeyedObjectPool<K,​V> checkedPool​(KeyedObjectPool<K,​V> keyedPool,
                                                                         Class<V> type)
        Wraps a KeyedObjectPool and dynamically checks the type of objects borrowed and returned to the keyedPool. If an object is passed to the keyedPool that isn't of type type a ClassCastException will be thrown.
        Type Parameters:
        K - the type of key
        V - the type of object
        Parameters:
        keyedPool - the keyedPool to enforce type safety on
        type - the class type to enforce.
        Returns:
        a KeyedObjectPool that will only allow objects of type
        Since:
        Pool 1.3
      • synchronizedPool

        public static <T> ObjectPool<T> synchronizedPool​(ObjectPool<T> pool)
        Returns a synchronized (thread-safe) ObjectPool backed by the specified ObjectPool.

        Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons Pool library. Wrapping a pool that waits for poolable objects to be returned before allowing another one to be borrowed with another layer of synchronization will cause liveliness issues or a deadlock.

        Type Parameters:
        T - the type of object
        Parameters:
        pool - the ObjectPool to be "wrapped" in a synchronized ObjectPool.
        Returns:
        a synchronized view of the specified ObjectPool.
        Since:
        Pool 1.3
      • synchronizedPool

        public static <K,​V> KeyedObjectPool<K,​V> synchronizedPool​(KeyedObjectPool<K,​V> keyedPool)
        Returns a synchronized (thread-safe) KeyedObjectPool backed by the specified KeyedObjectPool.

        Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons Pool library. Wrapping a pool that waits for poolable objects to be returned before allowing another one to be borrowed with another layer of synchronization will cause liveliness issues or a deadlock.

        Type Parameters:
        K - the type of key
        V - the type of object
        Parameters:
        keyedPool - the KeyedObjectPool to be "wrapped" in a synchronized KeyedObjectPool.
        Returns:
        a synchronized view of the specified KeyedObjectPool.
        Since:
        Pool 1.3
      • synchronizedPoolableFactory

        public static <T> PoolableObjectFactory<T> synchronizedPoolableFactory​(PoolableObjectFactory<T> factory)
        Returns a synchronized (thread-safe) PoolableObjectFactory backed by the specified PoolableObjectFactory.
        Type Parameters:
        T - the type of object
        Parameters:
        factory - the PoolableObjectFactory to be "wrapped" in a synchronized PoolableObjectFactory.
        Returns:
        a synchronized view of the specified PoolableObjectFactory.
        Since:
        Pool 1.3
      • synchronizedPoolableFactory

        public static <K,​V> KeyedPoolableObjectFactory<K,​V> synchronizedPoolableFactory​(KeyedPoolableObjectFactory<K,​V> keyedFactory)
        Returns a synchronized (thread-safe) KeyedPoolableObjectFactory backed by the specified KeyedPoolableObjectFactory.
        Type Parameters:
        K - the type of key
        V - the type of object
        Parameters:
        keyedFactory - the KeyedPoolableObjectFactory to be "wrapped" in a synchronized KeyedPoolableObjectFactory.
        Returns:
        a synchronized view of the specified KeyedPoolableObjectFactory.
        Since:
        Pool 1.3
      • erodingPool

        public static <T> ObjectPool<T> erodingPool​(ObjectPool<T> pool)
        Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.
        Type Parameters:
        T - the type of object
        Parameters:
        pool - the ObjectPool to be decorated so it shrinks it's idle count when possible.
        Returns:
        a pool that adaptively decreases it's size when idle objects are no longer needed.
        Since:
        Pool 1.4
        See Also:
        erodingPool(ObjectPool, float)
      • erodingPool

        public static <T> ObjectPool<T> erodingPool​(ObjectPool<T> pool,
                                                    float factor)
        Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.

        The factor parameter provides a mechanism to tweak the rate at which the pool tries to shrink it's size. Values between 0 and 1 cause the pool to try to shrink it's size more often. Values greater than 1 cause the pool to less frequently try to shrink it's size.

        Type Parameters:
        T - the type of object
        Parameters:
        pool - the ObjectPool to be decorated so it shrinks it's idle count when possible.
        factor - a positive value to scale the rate at which the pool tries to reduce it's size. If 0 < factor < 1 then the pool shrinks more aggressively. If 1 < factor then the pool shrinks less aggressively.
        Returns:
        a pool that adaptively decreases it's size when idle objects are no longer needed.
        Since:
        Pool 1.4
        See Also:
        erodingPool(ObjectPool)
      • erodingPool

        public static <K,​V> KeyedObjectPool<K,​V> erodingPool​(KeyedObjectPool<K,​V> keyedPool)
        Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.
        Type Parameters:
        K - the type of key
        V - the type of object
        Parameters:
        keyedPool - the KeyedObjectPool to be decorated so it shrinks it's idle count when possible.
        Returns:
        a pool that adaptively decreases it's size when idle objects are no longer needed.
        Since:
        Pool 1.4
        See Also:
        erodingPool(KeyedObjectPool, float), erodingPool(KeyedObjectPool, float, boolean)
      • erodingPool

        public static <K,​V> KeyedObjectPool<K,​V> erodingPool​(KeyedObjectPool<K,​V> keyedPool,
                                                                         float factor)
        Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.

        The factor parameter provides a mechanism to tweak the rate at which the pool tries to shrink it's size. Values between 0 and 1 cause the pool to try to shrink it's size more often. Values greater than 1 cause the pool to less frequently try to shrink it's size.

        Type Parameters:
        K - the type of key
        V - the type of object
        Parameters:
        keyedPool - the KeyedObjectPool to be decorated so it shrinks it's idle count when possible.
        factor - a positive value to scale the rate at which the pool tries to reduce it's size. If 0 < factor < 1 then the pool shrinks more aggressively. If 1 < factor then the pool shrinks less aggressively.
        Returns:
        a pool that adaptively decreases it's size when idle objects are no longer needed.
        Since:
        Pool 1.4
        See Also:
        erodingPool(KeyedObjectPool, float, boolean)
      • erodingPool

        public static <K,​V> KeyedObjectPool<K,​V> erodingPool​(KeyedObjectPool<K,​V> keyedPool,
                                                                         float factor,
                                                                         boolean perKey)
        Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.

        The factor parameter provides a mechanism to tweak the rate at which the pool tries to shrink it's size. Values between 0 and 1 cause the pool to try to shrink it's size more often. Values greater than 1 cause the pool to less frequently try to shrink it's size.

        The perKey parameter determines if the pool shrinks on a whole pool basis or a per key basis. When perKey is false, the keys do not have an effect on the rate at which the pool tries to shrink it's size. When perKey is true, each key is shrunk independently.

        Type Parameters:
        K - the type of key
        V - the type of object
        Parameters:
        keyedPool - the KeyedObjectPool to be decorated so it shrinks it's idle count when possible.
        factor - a positive value to scale the rate at which the pool tries to reduce it's size. If 0 < factor < 1 then the pool shrinks more aggressively. If 1 < factor then the pool shrinks less aggressively.
        perKey - when true, each key is treated independently.
        Returns:
        a pool that adaptively decreases it's size when idle objects are no longer needed.
        Since:
        Pool 1.4
        See Also:
        erodingPool(KeyedObjectPool), erodingPool(KeyedObjectPool, float)