Class Pool<T>


  • public class Pool<T>
    extends java.lang.Object
    Manages a fixed pool of resources (e.g. buffers). Uses the fast try-lock operation to get a resource from the pool, or returns a newly created resource. When the returned lock is unlocked, that resource becomes available again for consumption
    Since:
    3.5
    Author:
    Bela Ban
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Pool.Element<T>  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.function.Supplier<T> creator  
      protected java.util.concurrent.locks.Lock[] locks  
      protected T[] pool  
    • Constructor Summary

      Constructors 
      Constructor Description
      Pool​(int capacity, java.util.function.Supplier<T> creator)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Pool.Element<T> get()
      Gets the next available resource for which the lock can be acquired and returns it and its associated lock, which needs to be released when the caller is done using the resource.
      T[] getElements()  
      int getNumLocked()  
      static void main​(java.lang.String[] args)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • pool

        protected final T[] pool
      • locks

        protected final java.util.concurrent.locks.Lock[] locks
      • creator

        protected final java.util.function.Supplier<T> creator
    • Constructor Detail

      • Pool

        public Pool​(int capacity,
                    java.util.function.Supplier<T> creator)
    • Method Detail

      • getElements

        public T[] getElements()
      • getNumLocked

        public int getNumLocked()
      • get

        public Pool.Element<T> get()
        Gets the next available resource for which the lock can be acquired and returns it and its associated lock, which needs to be released when the caller is done using the resource. If no resource in the pool can be locked, returns a newly created resource and a null lock. This means that no lock was acquired and thus doesn't need to be released.
        Returns:
        An Element with T and a lock (possibly null if newly created)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • main

        public static void main​(java.lang.String[] args)