Class JArrayPool

java.lang.Object
net.algart.arrays.JArrayPool

public final class JArrayPool extends Object

A simple pool of the Java arrays (usually work buffers) with the same size and type of elements, based on a list of SoftReference. This class is useful in algorithms that frequently need to allocate little buffers (tens of kilobytes), because it allows to reduce time spent by the allocation of Java memory 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 JArrayPool getInstance(Class<?> elementType, int arrayLength)
      Creates the pool of Java arrays. Every array will have the given element type and length.
      Parameters:
      elementType - the type of elements in the arrays.
      arrayLength - the length of the arrays.
      Returns:
      new pool of Java arrays.
      Throws:
      NullPointerException - if elementType is null.
      IllegalArgumentException - if arrayLength is negative.
    • 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 int arrayLength()
      Returns the size of all arrays in this pool.
      Returns:
      the size of all arrays in this pool.
    • requestArray

      public Object requestArray()
      Returns the ready for use Java 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(Object) should be called after finishing working with this array.

      Returns:
      the ready for use Java array.
    • releaseArray

      public void releaseArray(Object array)
      Releases the Java 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 Java array, returned by previous requestArray() call; may be null, then the method does nothing.
      Throws:
      IllegalArgumentException - if the argument is not a Java array, or if its size or element type do not match the arguments of getInstance(java.lang.Class<?>, int) 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.