java.util.concurrent.atomic
Class AtomicReferenceArray<V>

java.lang.Object
  extended byjava.util.concurrent.atomic.AtomicReferenceArray
All Implemented Interfaces:
java.io.Serializable

public final class AtomicReferenceArray<V>
extends java.lang.Object
implements java.io.Serializable

An AtomicReferenceArray maintains an array of object references in which elements may be updated atomically, and are accessed using volatile semantics.

Since:
1.5
Author:
Doug Lea
See Also:
Serialized Form

Constructor Summary
AtomicReferenceArray(int length)
          Create a new AtomicReferenceArray of given length.
 
Method Summary
 boolean compareAndSet(int i, V expect, V update)
          Atomically set the value to the given updated value if the current value == the expected value.
 V get(int i)
          Get the current value at position i.
 V getAndSet(int i, V newValue)
          Set the element at position i to the given value and return the old value.
 int length()
          Return the length of the array.
 void set(int i, V newValue)
          Set the element at position i to the given value.
 boolean weakCompareAndSet(int i, V expect, V update)
          Atomically set the value to the given updated value if the current value == the expected value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AtomicReferenceArray

public AtomicReferenceArray(int length)
Create a new AtomicReferenceArray of given length.

Parameters:
length - the length of the array
Method Detail

length

public int length()
Return the length of the array.


get

public V get(int i)
Get the current value at position i.

Parameters:
i - the index
Returns:
the current value

set

public void set(int i,
                V newValue)
Set the element at position i to the given value.

Parameters:
i - the index
newValue - the new value

getAndSet

public V getAndSet(int i,
                   V newValue)
Set the element at position i to the given value and return the old value.

Parameters:
i - the index
newValue - the new value
Returns:
the previous value

compareAndSet

public boolean compareAndSet(int i,
                             V expect,
                             V update)
Atomically set the value to the given updated value if the current value == the expected value.

Parameters:
i - the index
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the actual value was not equal to the expected value.

weakCompareAndSet

public boolean weakCompareAndSet(int i,
                                 V expect,
                                 V update)
Atomically set the value to the given updated value if the current value == the expected value. May fail spuriously.

Parameters:
i - the index
expect - the expected value
update - the new value
Returns:
true if successful.