Class Pool<T>

java.lang.Object
io.javago.sync.Pool<T>
Type Parameters:
T - the type of objects managed by the pool

public class Pool<T> extends Object
The Pool class implements Go's sync.Pool. A thread-safe object pool that manages a collection of reusable objects.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Pool(Supplier<T> creator)
    Constructs a new Pool with the given object creator.
  • Method Summary

    Modifier and Type
    Method
    Description
    get()
    Retrieves an object from the pool.
    void
    put(T t)
    Returns an object to the pool, making it available for future retrieval.

    Methods inherited from class java.lang.Object

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

    • Pool

      public Pool(Supplier<T> creator)
      Constructs a new Pool with the given object creator.
      Parameters:
      creator - a Supplier that provides new instances of the objects managed by the pool
  • Method Details

    • get

      public T get()
      Retrieves an object from the pool. If the pool is empty, a new object is created using the Supplier.
      Returns:
      an object from the pool, or a newly created object if the pool is empty
    • put

      public void put(T t)
      Returns an object to the pool, making it available for future retrieval. If the number of objects in the pool is equal to Integer.MAX_VALUE, the object will not be returned to the pool.
      Parameters:
      t - the object to be returned to the pool