|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.dautelle.realtime.ArrayPool
This class provides static methods to return ObjectPool
for
array of any type. The pool returned allocates arrays from the "stack"
when the current thread executes in PoolContext
.
Allocating large arrays is very time consuming and prone to garbage collection interrruptions. It is therefore highly recommended to use this class when new arrays are dynamically allocated.
Applications may create arrays of any component type including primitive types. For example:
char[] chars = (char[]) ArrayPool.charArray(128).next(); Thread[] threads = (Thread[]) ArrayPool.array(Thread.class, 64).next();The arrays returned may have a length larger than the specified capacity. If the exact length is required then custom
ObjectFactory
should
be used instead.
Arrays may be individually recycled when it can be asserted that the arrays will not be referenced anymore. For example:
ObjectPool pool = ArrayPool.charArray(1024); char[] buffer = (char[]) pool.next(); for (int i = reader.read(buffer, 0, buffer.length); i > 0;) { ... } pool.recycle(buffer);Note: Array allocation/recycling is effective only if the current thread executes within a
PoolContext
.
"Stack" allocated arrays are often used to implement dynamically sized
Realtime
objects. For example:
public class FastString extends RealtimeObject implements CharSequence { private static final Factory FACTORY = new Factory() { public Object create() { return new FastString(); } }; char[] _data; // Dynamic data buffer. int _first, _length; public static FastString newInstance(int capacity) { FastString str = (FastString) FACTORY.object(); str._data = (char[]) ArrayPool.charArray(capacity).next(); str._first = 0; str._length = 0; return str; } }
Field Summary | |
static int |
MIN_LENGTH
Holds the minimum length array ( 16 ). |
Method Summary | |
static ObjectPool |
array(java.lang.Class componentType,
int capacity)
Returns the current pool for arrays of specified component type and minimum capacity. |
static ObjectPool |
byteArray(int capacity)
Returns the current pool for byte[] . |
static ObjectPool |
charArray(int capacity)
Returns the current pool for char[] . |
static void |
clear(java.lang.Object[] array,
int start,
int length)
Clears an array beginning at the specified position. |
static ObjectPool |
doubleArray(int capacity)
Returns the current pool for double[] . |
static ObjectPool |
floatArray(int capacity)
Returns the current pool for float[] . |
static java.lang.Object |
heapCopy(java.lang.Object array,
int first,
int length)
Copies the specified array to the heap. |
static int |
indexFor(int capacity)
Returns a factory index (0-27) for the specified capacity. |
static ObjectPool |
intArray(int capacity)
Returns the current pool for int[] . |
static ObjectPool |
longArray(int capacity)
Returns the current pool for long[] . |
static ObjectPool |
objectArray(int capacity)
Returns the current pool for Object[] . |
static java.lang.Object |
outerCopy(java.lang.Object array,
int first,
int length)
Copies the specified array into an array from the outer pool. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int MIN_LENGTH
16
).
Method Detail |
public static java.lang.Object outerCopy(java.lang.Object array, int first, int length)
ObjectFactory
.
For example:public class FastString extends RealtimeObject implements CharSequence { ... public Object export() { if (isLocalObject()) { super.export(); _data = (char[]) ArrayPool.outerCopy(_data, _first, _length); _first = 0; } // Else non-local object cannot refer to local object. return this; } }
array
- the array to copy.first
- the index of the first component to copy
(will be at position 0
in the copy).length
- the number of components to copy.
java.lang.IllegalArgumentException
- if array is not an array.public static java.lang.Object heapCopy(java.lang.Object array, int first, int length)
ObjectFactory
.
For example:public class FastString extends RealtimeObject implements CharSequence { ... public Object toHeap() { if (isPoolObject()) { super.toHeap(); _data = (char[]) ArrayPool.heapCopy(_data, _first, _length); _first = 0; } // Else heap objects can only refer to heap objects. return this; } }
array
- the array to copy.first
- the index of the first component to copy
(will be at position 0
in the copy).length
- the number of components to copy.
java.lang.IllegalArgumentException
- if array is not an array.public static void clear(java.lang.Object[] array, int start, int length)
array
- the array whose components are set to null
.start
- the the starting position.length
- the number of components to clear.
java.lang.IndexOutOfBoundsException
- if clearing would cause access of data
outside array bounds.public static ObjectPool array(java.lang.Class componentType, int capacity)
componentType
- the Class
object representing the
component type of the array.capacity
- the minimum length of the array.
public static ObjectPool objectArray(int capacity)
Object[]
.
capacity
- the minimum length of the array.
public static ObjectPool byteArray(int capacity)
byte[]
.
capacity
- the minimum length of the array.
public static ObjectPool charArray(int capacity)
char[]
.
capacity
- the minimum length of the array.
public static ObjectPool intArray(int capacity)
int[]
.
capacity
- the minimum length of the array.
public static ObjectPool longArray(int capacity)
long[]
.
capacity
- the minimum length of the array.
public static ObjectPool floatArray(int capacity)
float[]
.
capacity
- the minimum length of the array.
public static ObjectPool doubleArray(int capacity)
double[]
.
capacity
- the minimum length of the array.
public static int indexFor(int capacity)
capacity
- the required capacity.
j
such as
(MIN_LENGTH
<< j) >= capacity
|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |