2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
7 package java.util.concurrent.atomic;
8 import sun.misc.Unsafe;
12 * An array of object references in which elements may be updated
13 * atomically. See the {@link java.util.concurrent.atomic} package
14 * specification for description of the properties of atomic
18 * @param <E> The base class of elements held in this array
20 public class AtomicReferenceArray<E> implements java.io.Serializable {
21 private static final long serialVersionUID = -6209656149925076980L;
23 private static final Unsafe unsafe = Unsafe.getUnsafe();
24 private static final int base = unsafe.arrayBaseOffset(Object[].class);
25 private static final int scale = unsafe.arrayIndexScale(Object[].class);
26 private final Object[] array;
28 private long rawIndex(int i) {
29 if (i < 0 || i >= array.length)
30 throw new IndexOutOfBoundsException("index " + i);
31 return base + i * scale;
35 * Creates a new AtomicReferenceArray of given length.
36 * @param length the length of the array
38 public AtomicReferenceArray(int length) {
39 array = new Object[length];
40 // must perform at least one volatile write to conform to JMM
42 unsafe.putObjectVolatile(array, rawIndex(0), null);
46 * Creates a new AtomicReferenceArray with the same length as, and
47 * all elements copied from, the given array.
49 * @param array the array to copy elements from
50 * @throws NullPointerException if array is null
52 public AtomicReferenceArray(E[] array) {
54 throw new NullPointerException();
55 int length = array.length;
56 this.array = new Object[length];
59 for (int i = 0; i < last; ++i)
60 this.array[i] = array[i];
61 // Do the last write as volatile
63 unsafe.putObjectVolatile(this.array, rawIndex(last), e);
68 * Returns the length of the array.
70 * @return the length of the array
72 public final int length() {
77 * Gets the current value at position <tt>i</tt>.
80 * @return the current value
82 public final E get(int i) {
83 return (E) unsafe.getObjectVolatile(array, rawIndex(i));
87 * Sets the element at position <tt>i</tt> to the given value.
90 * @param newValue the new value
92 public final void set(int i, E newValue) {
93 unsafe.putObjectVolatile(array, rawIndex(i), newValue);
97 * Eventually sets the element at position <tt>i</tt> to the given value.
100 * @param newValue the new value
103 public final void lazySet(int i, E newValue) {
104 unsafe.putOrderedObject(array, rawIndex(i), newValue);
109 * Atomically sets the element at position <tt>i</tt> to the given
110 * value and returns the old value.
113 * @param newValue the new value
114 * @return the previous value
116 public final E getAndSet(int i, E newValue) {
119 if (compareAndSet(i, current, newValue))
125 * Atomically sets the element at position <tt>i</tt> to the given
126 * updated value if the current value <tt>==</tt> the expected value.
128 * @param expect the expected value
129 * @param update the new value
130 * @return true if successful. False return indicates that
131 * the actual value was not equal to the expected value.
133 public final boolean compareAndSet(int i, E expect, E update) {
134 return unsafe.compareAndSwapObject(array, rawIndex(i),
139 * Atomically sets the element at position <tt>i</tt> to the given
140 * updated value if the current value <tt>==</tt> the expected value.
141 * May fail spuriously and does not provide ordering guarantees,
142 * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
145 * @param expect the expected value
146 * @param update the new value
147 * @return true if successful.
149 public final boolean weakCompareAndSet(int i, E expect, E update) {
150 return compareAndSet(i, expect, update);
154 * Returns the String representation of the current values of array.
155 * @return the String representation of the current values of array.
157 public String toString() {
158 if (array.length > 0) // force volatile read
160 return Arrays.toString(array);