KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)AtomicLongArray.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  * A <tt>long</tt> array in which elements may be updated atomically.
14  * See the {@link java.util.concurrent.atomic} package specification
15  * for description of the properties of atomic variables.
16  * @since 1.5
17  * @author Doug Lea
18  */

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

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

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

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

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

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

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

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

134     public final boolean weakCompareAndSet(int i, long expect, long update) {
135         return compareAndSet(i, expect, update);
136     }
137
138     /**
139      * Atomically increment by one the element at index <tt>i</tt>.
140      *
141      * @param i the index
142      * @return the previous value;
143      */

144     public final long getAndIncrement(int i) {
145         while (true) {
146             long current = get(i);
147             long next = current + 1;
148             if (compareAndSet(i, current, next))
149                 return current;
150         }
151     }
152   
153     /**
154      * Atomically decrement by one the element at index <tt>i</tt>.
155      *
156      * @param i the index
157      * @return the previous value;
158      */

159     public final long getAndDecrement(int i) {
160         while (true) {
161             long current = get(i);
162             long next = current - 1;
163             if (compareAndSet(i, current, next))
164                 return current;
165         }
166     }
167   
168     /**
169      * Atomically add the given value to element at index <tt>i</tt>.
170      *
171      * @param i the index
172      * @param delta the value to add
173      * @return the previous value;
174      */

175     public final long getAndAdd(int i, long delta) {
176         while (true) {
177             long current = get(i);
178             long next = current + delta;
179             if (compareAndSet(i, current, next))
180                 return current;
181         }
182     }
183   
184
185     /**
186      * Atomically increment the element at index <tt>i</tt>.
187      *
188      * @param i the index
189      * @return the updated value;
190      */

191     public final long incrementAndGet(int i) {
192         while (true) {
193             long current = get(i);
194             long next = current + 1;
195             if (compareAndSet(i, current, next))
196                 return next;
197         }
198     }
199   
200     /**
201      * Atomically decrement the element at index <tt>i</tt>.
202      *
203      * @param i the index
204      * @return the updated value;
205      */

206     public final long decrementAndGet(int i) {
207         while (true) {
208             long current = get(i);
209             long next = current - 1;
210             if (compareAndSet(i, current, next))
211                 return next;
212         }
213     }
214   
215     /**
216      * Atomically add the given value to element at index <tt>i</tt>.
217      *
218      * @param i the index
219      * @param delta the value to add
220      * @return the updated value;
221      */

222     public long addAndGet(int i, long delta) {
223         while (true) {
224             long current = get(i);
225             long next = current + delta;
226             if (compareAndSet(i, current, next))
227                 return next;
228         }
229     }
230
231     /**
232      * Returns the String representation of the current values of array.
233      * @return the String representation of the current values of array.
234      */

235     public String JavaDoc toString() {
236         if (array.length > 0) // force volatile read
237
get(0);
238         return Arrays.toString(array);
239     }
240   
241 }
242
Popular Tags