FileDocCategorySizeDatePackage
ThreadSafeIntList.javaAPI DocExample2900Sat Jan 24 10:44:26 GMT 2004je3.thread

ThreadSafeIntList

public 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 voidadd(int value)

 
	if (size == data.length) setCapacity(size*2); // realloc if necessary
	data[size++] = value;                         // add value to list
    
public synchronized voidclear()

 size = 0; 
public synchronized intget(int index)

	if (index < 0 || index >= size) // Check that argument is legitimate
	    throw new IndexOutOfBoundsException(String.valueOf(index));
	return data[index];
    
protected voidsetCapacity(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 intsize()

 return size; 
public synchronized int[]toArray()

 
	int[] copy = new int[size];
	System.arraycopy(data, 0, copy, 0, size);
	return copy;