Class SoftPool<T>

java.lang.Object
org.jsoup.internal.SoftPool<T>
Type Parameters:
T - the type of object to pool.

public class SoftPool<T> extends Object
A SoftPool is a ThreadLocal that holds a SoftReference to a pool of initializable objects. This allows us to reuse expensive objects (buffers, etc.) between invocations (the ThreadLocal), but also for those objects to be reaped if they are no longer in use.

Like a ThreadLocal, should be stored in a static field.

Since:
1.18.2
  • Constructor Summary

    Constructors
    Constructor
    Description
    SoftPool(Supplier<T> initializer)
    Create a new SoftPool.
  • Method Summary

    Modifier and Type
    Method
    Description
    T
    borrow()
    Borrow an object from the pool, creating a new one if the pool is empty.
    void
    release(T value)
    Release an object back to the pool.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SoftPool

      public SoftPool(Supplier<T> initializer)
      Create a new SoftPool.
      Parameters:
      initializer - a supplier that creates a new object when one is needed.
  • Method Details

    • borrow

      public T borrow()
      Borrow an object from the pool, creating a new one if the pool is empty. Make sure to release it back to the pool when done, so that it can be reused.
      Returns:
      an object from the pool, as defined by the initializer.
    • release

      public void release(T value)
      Release an object back to the pool. If the pool is full, the object is not retained. If you don't want to reuse a borrowed object (for e.g. a StringBuilder that grew too large), just don't release it.
      Parameters:
      value - the object to release back to the pool.