ThreadSafeIntListpublic class ThreadSafeIntList extends Object A growable array of int values, suitable for use with multiple threads. |
Fields Summary |
---|
protected int[] | data | protected int | size | private static final int | DEFAULT_CAPACITY |
Constructors Summary |
---|
public ThreadSafeIntList()
// Create a ThreadSafeIntList with a default capacity
// We don't have to set size to zero because newly created objects
// automatically have their fields set to zero, false, and null.
data = new int[DEFAULT_CAPACITY]; // Allocate the array
| public ThreadSafeIntList(ThreadSafeIntList original)
synchronized(original) {
this.data = (int[]) original.data.clone();
this.size = original.size;
}
|
Methods Summary |
---|
public synchronized void | add(int value)
if (size == data.length) setCapacity(size*2); // realloc if necessary
data[size++] = value; // add value to list
| public synchronized void | clear() size = 0;
| public synchronized int | get(int index)
if (index < 0 || index >= size) // Check that argument is legitimate
throw new IndexOutOfBoundsException(String.valueOf(index));
return data[index];
| protected void | setCapacity(int n)
if (n == data.length) return; // Check size
int[] newdata = new int[n]; // Allocate the new array
System.arraycopy(data, 0, newdata, 0, size); // Copy data into it
data = newdata; // Replace old array
| public synchronized int | size() return size;
| public synchronized int[] | toArray()
int[] copy = new int[size];
System.arraycopy(data, 0, copy, 0, size);
return copy;
|
|