Class DefaultPooledObject<T>

  • Type Parameters:
    T - the type of object in the pool
    All Implemented Interfaces:
    java.lang.Comparable<PooledObject<T>>, PooledObject<T>
    Direct Known Subclasses:
    PooledSoftReference

    public class DefaultPooledObject<T>
    extends java.lang.Object
    implements PooledObject<T>
    This wrapper is used to track the additional information, such as state, for the pooled objects.

    This class is intended to be thread-safe.

    Since:
    2.0
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultPooledObject​(T object)
      Creates a new instance that wraps the provided object so that the pool can track the state of the pooled object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean allocate()
      Allocates the object.
      int compareTo​(PooledObject<T> other)
      Orders instances based on idle time - i.e. the length of time since the instance was returned to the pool.
      boolean deallocate()
      Deallocates the object and sets it IDLE if it is currently ALLOCATED or RETURNING.
      boolean endEvictionTest​(java.util.Deque<PooledObject<T>> idleQueue)
      Called to inform the object that the eviction test has ended.
      long getActiveTimeMillis()
      Obtains the time in milliseconds that this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).
      long getBorrowedCount()
      Gets the number of times this object has been borrowed.
      long getCreateTime()
      Obtains the time (using the same basis as System.currentTimeMillis()) that this object was created.
      long getIdleTimeMillis()
      Obtains the time in milliseconds that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).
      long getLastBorrowTime()
      Obtains the time the wrapped object was last borrowed.
      long getLastReturnTime()
      Obtains the time the wrapped object was last returned.
      long getLastUsedTime()
      Returns an estimate of the last time this object was used.
      T getObject()
      Obtains the underlying object that is wrapped by this instance of PooledObject.
      PooledObjectState getState()
      Returns the state of this object.
      void invalidate()
      Sets the state to INVALID
      void markAbandoned()
      Marks the pooled object as abandoned.
      void markReturning()
      Marks the object as returning to the pool.
      void printStackTrace​(java.io.PrintWriter writer)
      Prints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.
      void setLogAbandoned​(boolean logAbandoned)
      Is abandoned object tracking being used?
      void setRequireFullStackTrace​(boolean requireFullStackTrace)
      Configures the stack trace generation strategy based on whether or not fully detailed stack traces are required.
      boolean startEvictionTest()
      Attempts to place the pooled object in the PooledObjectState.EVICTION state.
      java.lang.String toString()
      Provides a String form of the wrapper for debug purposes.
      void use()
      Record the current stack trace as the last time the object was used.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • DefaultPooledObject

        public DefaultPooledObject​(T object)
        Creates a new instance that wraps the provided object so that the pool can track the state of the pooled object.
        Parameters:
        object - The object to wrap
    • Method Detail

      • getObject

        public T getObject()
        Description copied from interface: PooledObject
        Obtains the underlying object that is wrapped by this instance of PooledObject.
        Specified by:
        getObject in interface PooledObject<T>
        Returns:
        The wrapped object
      • getCreateTime

        public long getCreateTime()
        Description copied from interface: PooledObject
        Obtains the time (using the same basis as System.currentTimeMillis()) that this object was created.
        Specified by:
        getCreateTime in interface PooledObject<T>
        Returns:
        The creation time for the wrapped object
      • getActiveTimeMillis

        public long getActiveTimeMillis()
        Description copied from interface: PooledObject
        Obtains the time in milliseconds that this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).
        Specified by:
        getActiveTimeMillis in interface PooledObject<T>
        Returns:
        The time in milliseconds last spent in the active state
      • getIdleTimeMillis

        public long getIdleTimeMillis()
        Description copied from interface: PooledObject
        Obtains the time in milliseconds that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).
        Specified by:
        getIdleTimeMillis in interface PooledObject<T>
        Returns:
        The time in milliseconds last spent in the idle state
      • getLastBorrowTime

        public long getLastBorrowTime()
        Description copied from interface: PooledObject
        Obtains the time the wrapped object was last borrowed.
        Specified by:
        getLastBorrowTime in interface PooledObject<T>
        Returns:
        The time the object was last borrowed
      • getLastReturnTime

        public long getLastReturnTime()
        Description copied from interface: PooledObject
        Obtains the time the wrapped object was last returned.
        Specified by:
        getLastReturnTime in interface PooledObject<T>
        Returns:
        The time the object was last returned
      • getBorrowedCount

        public long getBorrowedCount()
        Gets the number of times this object has been borrowed.
        Specified by:
        getBorrowedCount in interface PooledObject<T>
        Returns:
        The number of times this object has been borrowed.
        Since:
        2.1
      • compareTo

        public int compareTo​(PooledObject<T> other)
        Description copied from interface: PooledObject
        Orders instances based on idle time - i.e. the length of time since the instance was returned to the pool. Used by the GKOP idle object evictor.

        Note: This class has a natural ordering that is inconsistent with equals if distinct objects have the same identity hash code.

        Specified by:
        compareTo in interface java.lang.Comparable<T>
        Specified by:
        compareTo in interface PooledObject<T>
      • toString

        public java.lang.String toString()
        Description copied from interface: PooledObject
        Provides a String form of the wrapper for debug purposes. The format is not fixed and may change at any time.

        Specified by:
        toString in interface PooledObject<T>
        Overrides:
        toString in class java.lang.Object
      • endEvictionTest

        public boolean endEvictionTest​(java.util.Deque<PooledObject<T>> idleQueue)
        Description copied from interface: PooledObject
        Called to inform the object that the eviction test has ended.
        Specified by:
        endEvictionTest in interface PooledObject<T>
        Parameters:
        idleQueue - The queue of idle objects to which the object should be returned
        Returns:
        Currently not used
      • allocate

        public boolean allocate()
        Allocates the object.
        Specified by:
        allocate in interface PooledObject<T>
        Returns:
        true if the original state was IDLE
      • use

        public void use()
        Description copied from interface: PooledObject
        Record the current stack trace as the last time the object was used.
        Specified by:
        use in interface PooledObject<T>
      • printStackTrace

        public void printStackTrace​(java.io.PrintWriter writer)
        Description copied from interface: PooledObject
        Prints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.
        Specified by:
        printStackTrace in interface PooledObject<T>
        Parameters:
        writer - The destination for the debug output
      • markAbandoned

        public void markAbandoned()
        Marks the pooled object as abandoned.
        Specified by:
        markAbandoned in interface PooledObject<T>
      • markReturning

        public void markReturning()
        Marks the object as returning to the pool.
        Specified by:
        markReturning in interface PooledObject<T>
      • setLogAbandoned

        public void setLogAbandoned​(boolean logAbandoned)
        Description copied from interface: PooledObject
        Is abandoned object tracking being used? If this is true the implementation will need to record the stack trace of the last caller to borrow this object.
        Specified by:
        setLogAbandoned in interface PooledObject<T>
        Parameters:
        logAbandoned - The new configuration setting for abandoned object tracking
      • setRequireFullStackTrace

        public void setRequireFullStackTrace​(boolean requireFullStackTrace)
        Configures the stack trace generation strategy based on whether or not fully detailed stack traces are required. When set to false, abandoned logs may only include caller class information rather than method names, line numbers, and other normal metadata available in a full stack trace.
        Specified by:
        setRequireFullStackTrace in interface PooledObject<T>
        Parameters:
        requireFullStackTrace - the new configuration setting for abandoned object logging
        Since:
        2.5