Class ArrayPool

java.lang.Object
net.algart.arrays.ArrayPool

public class ArrayPool extends Object

A simple pool of the unresizable AlgART arrays (usually work buffers) with the same size and type of elements, based on a list of SoftReference or WeakReference. This class is useful in algorithms that frequently need to allocate buffers in AlgART arrays, because it allows to reduce time spent by the allocation of AlgART arrays and the garbage collection.

This class is thread-safe: you may use the same instance of this class in several threads.

Author:
Daniel Alievsky
See Also:
  • Method Details

    • getInstance

      public static ArrayPool getInstance(MemoryModel memoryModel, Class<?> elementType, long arrayLength)
      Creates the pool of AlgART arrays. Every array will have the given element type and length.
      Parameters:
      memoryModel - the memory model that will be used for allocating new arrays.
      elementType - the type of elements in the arrays.
      arrayLength - the length of the arrays.
      Returns:
      new pool of AlgART arrays.
      Throws:
      NullPointerException - if memoryModel or elementType is null.
      IllegalArgumentException - if arrayLength is negative.
    • memoryModel

      public MemoryModel memoryModel()
      Returns the memory model used for allocating new arrays.
      Returns:
      the memory model used for allocating new arrays.
    • elementType

      public Class<?> elementType()
      Returns the type of elements in the arrays in this pool.
      Returns:
      the type of elements in the arrays in this pool.
    • arrayLength

      public long arrayLength()
      Returns the size of all arrays in this pool.
      Returns:
      the size of all arrays in this pool.
    • requestArray

      public UpdatableArray requestArray()
      Returns the ready for use unresizable AlgART array. If it is not found in the internal cache, it is created, in other case some free array from the cache is returned.

      The releaseArray(UpdatableArray) should be called after finishing working with this array.

      Returns:
      the ready for use AlgART array.
    • releaseArray

      public void releaseArray(UpdatableArray array)
      Releases the AlgART array, returned by previous requestArray() call, and adds it to the internal cache of arrays. Future calls of requestArray(), maybe, will return it again.

      Please note: it will not be an error if you will not call this method after requestArray(). But calling this method improves performance of future requestArray() calls.

      This method must not be called twice for the same object.

      This method does nothing if the passed argument is null.

      Parameters:
      array - some AlgART array, returned by previous requestArray() call; may be null, then the method does nothing.
      Throws:
      IllegalArgumentException - if the length or element type of the passed array do not match the arguments of getInstance method.
    • toString

      public String toString()
      Returns a brief string description of this object.

      The result of this method may depend on implementation.

      Overrides:
      toString in class Object
      Returns:
      a brief string description of this object.