Class ArrayPool
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 Summary
Modifier and TypeMethodDescriptionlong
Returns the size of all arrays in this pool.Class<?>
Returns the type of elements in the arrays in this pool.static ArrayPool
getInstance
(MemoryModel memoryModel, Class<?> elementType, long arrayLength) Creates the pool of AlgART arrays.Returns the memory model used for allocating new arrays.void
releaseArray
(UpdatableArray array) Releases the AlgART array, returned by previousrequestArray()
call, and adds it to the internal cache of arrays.Returns the ready for use unresizable AlgART array.toString()
Returns a brief string description of this object.
-
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
Returns the memory model used for allocating new arrays.- Returns:
- the memory model used for allocating new arrays.
-
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
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
Releases the AlgART 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 AlgART array, returned by previousrequestArray()
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 ofgetInstance
method.
-
toString
Returns a brief string description of this object.The result of this method may depend on implementation.
-