Class JArrayPool
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 Summary
Modifier and TypeMethodDescriptionint
Returns the size of all arrays in this pool.Class<?>
Returns the type of elements in the arrays in this pool.static JArrayPool
getInstance
(Class<?> elementType, int arrayLength) Creates the pool of Java arrays.void
releaseArray
(Object array) Releases the Java array, returned by previousrequestArray()
call, and adds it to the internal cache of arrays.Returns the ready for use Java array.toString()
Returns a brief string description of this object.
-
Method Details
-
getInstance
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
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
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
Releases the Java array, returned by previousrequestArray()
call, and adds it to the internal cache of arrays. Future calls ofrequestArray()
, 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 futurerequestArray()
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 previousrequestArray()
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 ofgetInstance(java.lang.Class<?>, int)
method.
-
toString
Returns a brief string description of this object.The result of this method may depend on implementation.
-