KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > concurrent > atomic > AtomicReferenceArray


1 /*
2  * @(#)AtomicReferenceArray.java 1.6 04/01/24
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.concurrent.atomic;
9 import sun.misc.Unsafe;
10 import java.util.*;
11
12 /**
13  * An array of object references in which elements may be updated
14  * atomically. See the {@link java.util.concurrent.atomic} package
15  * specification for description of the properties of atomic
16  * variables.
17  * @since 1.5
18  * @author Doug Lea
19  * @param <E> The base class of elements held in this array
20  */

21 public class AtomicReferenceArray<E> implements java.io.Serializable JavaDoc {
22     private static final long serialVersionUID = -6209656149925076980L;
23
24     private static final Unsafe unsafe = Unsafe.getUnsafe();
25     private static final int base = unsafe.arrayBaseOffset(Object JavaDoc[].class);
26     private static final int scale = unsafe.arrayIndexScale(Object JavaDoc[].class);
27     private final Object JavaDoc[] array;
28
29     private long rawIndex(int i) {
30         if (i < 0 || i >= array.length)
31             throw new IndexOutOfBoundsException JavaDoc("index " + i);
32         return base + i * scale;
33     }
34
35     /**
36      * Create a new AtomicReferenceArray of given length.
37      * @param length the length of the array
38      */

39     public AtomicReferenceArray(int length) {
40         array = new Object JavaDoc[length];
41         // must perform at least one volatile write to conform to JMM
42
if (length > 0)
43             unsafe.putObjectVolatile(array, rawIndex(0), null);
44     }
45
46     /**
47      * Create a new AtomicReferenceArray with the same length as, and
48      * all elements copied from, the given array.
49      *
50      * @param array the array to copy elements from
51      * @throws NullPointerException if array is null
52      */

53     public AtomicReferenceArray(E[] array) {
54         if (array == null)
55             throw new NullPointerException JavaDoc();
56         int length = array.length;
57         this.array = new Object JavaDoc[length];
58         if (length > 0) {
59             int last = length-1;
60             for (int i = 0; i < last; ++i)
61                 this.array[i] = array[i];
62             // Do the last write as volatile
63
E e = array[last];
64             unsafe.putObjectVolatile(this.array, rawIndex(last), e);
65         }
66     }
67
68     /**
69      * Returns the length of the array.
70      *
71      * @return the length of the array
72      */

73     public final int length() {
74         return array.length;
75     }
76
77     /**
78      * Get the current value at position <tt>i</tt>.
79      *
80      * @param i the index
81      * @return the current value
82      */

83     public final E get(int i) {
84         return (E) unsafe.getObjectVolatile(array, rawIndex(i));
85     }
86  
87     /**
88      * Set the element at position <tt>i</tt> to the given value.
89      *
90      * @param i the index
91      * @param newValue the new value
92      */

93     public final void set(int i, E newValue) {
94         unsafe.putObjectVolatile(array, rawIndex(i), newValue);
95     }
96   
97     /**
98      * Set the element at position <tt>i</tt> to the given value and return the
99      * old value.
100      *
101      * @param i the index
102      * @param newValue the new value
103      * @return the previous value
104      */

105     public final E getAndSet(int i, E newValue) {
106         while (true) {
107             E current = get(i);
108             if (compareAndSet(i, current, newValue))
109                 return current;
110         }
111     }
112   
113     /**
114      * Atomically set the value to the given updated value
115      * if the current value <tt>==</tt> the expected value.
116      * @param i the index
117      * @param expect the expected value
118      * @param update the new value
119      * @return true if successful. False return indicates that
120      * the actual value was not equal to the expected value.
121      */

122     public final boolean compareAndSet(int i, E expect, E update) {
123         return unsafe.compareAndSwapObject(array, rawIndex(i),
124                                          expect, update);
125     }
126
127     /**
128      * Atomically set the value to the given updated value
129      * if the current value <tt>==</tt> the expected value.
130      * May fail spuriously.
131      * @param i the index
132      * @param expect the expected value
133      * @param update the new value
134      * @return true if successful.
135      */

136     public final boolean weakCompareAndSet(int i, E expect, E update) {
137         return compareAndSet(i, expect, update);
138     }
139
140     /**
141      * Returns the String representation of the current values of array.
142      * @return the String representation of the current values of array.
143      */

144     public String JavaDoc toString() {
145         if (array.length > 0) // force volatile read
146
get(0);
147         return Arrays.toString(array);
148     }
149
150 }
151
Popular Tags