1 7 8 package java.util.concurrent.atomic; 9 import sun.misc.Unsafe; 10 import java.util.*; 11 12 20 public class AtomicIntegerArray implements java.io.Serializable { 21 private static final long serialVersionUID = 2862133569453604235L; 22 23 private static final Unsafe unsafe = Unsafe.getUnsafe(); 25 private static final int base = unsafe.arrayBaseOffset(int[].class); 26 private static final int scale = unsafe.arrayIndexScale(int[].class); 27 private final int[] array; 28 29 private long rawIndex(int i) { 30 if (i < 0 || i >= array.length) 31 throw new IndexOutOfBoundsException ("index " + i); 32 return base + i * scale; 33 } 34 35 40 public AtomicIntegerArray(int length) { 41 array = new int[length]; 42 if (length > 0) 44 unsafe.putIntVolatile(array, rawIndex(0), 0); 45 } 46 47 54 public AtomicIntegerArray(int[] array) { 55 if (array == null) 56 throw new NullPointerException (); 57 int length = array.length; 58 this.array = new int[length]; 59 if (length > 0) { 60 int last = length-1; 61 for (int i = 0; i < last; ++i) 62 this.array[i] = array[i]; 63 unsafe.putIntVolatile(this.array, rawIndex(last), array[last]); 65 } 66 } 67 68 73 public final int length() { 74 return array.length; 75 } 76 77 83 public final int get(int i) { 84 return unsafe.getIntVolatile(array, rawIndex(i)); 85 } 86 87 93 public final void set(int i, int newValue) { 94 unsafe.putIntVolatile(array, rawIndex(i), newValue); 95 } 96 97 105 public final int getAndSet(int i, int newValue) { 106 while (true) { 107 int current = get(i); 108 if (compareAndSet(i, current, newValue)) 109 return current; 110 } 111 } 112 113 123 public final boolean compareAndSet(int i, int expect, int update) { 124 return unsafe.compareAndSwapInt(array, rawIndex(i), 125 expect, update); 126 } 127 128 138 public final boolean weakCompareAndSet(int i, int expect, int update) { 139 return compareAndSet(i, expect, update); 140 } 141 142 148 public final int getAndIncrement(int i) { 149 while (true) { 150 int current = get(i); 151 int next = current + 1; 152 if (compareAndSet(i, current, next)) 153 return current; 154 } 155 } 156 157 163 public final int getAndDecrement(int i) { 164 while (true) { 165 int current = get(i); 166 int next = current - 1; 167 if (compareAndSet(i, current, next)) 168 return current; 169 } 170 } 171 172 179 public final int getAndAdd(int i, int delta) { 180 while (true) { 181 int current = get(i); 182 int next = current + delta; 183 if (compareAndSet(i, current, next)) 184 return current; 185 } 186 } 187 188 194 public final int incrementAndGet(int i) { 195 while (true) { 196 int current = get(i); 197 int next = current + 1; 198 if (compareAndSet(i, current, next)) 199 return next; 200 } 201 } 202 203 209 public final int decrementAndGet(int i) { 210 while (true) { 211 int current = get(i); 212 int next = current - 1; 213 if (compareAndSet(i, current, next)) 214 return next; 215 } 216 } 217 218 225 public final int addAndGet(int i, int delta) { 226 while (true) { 227 int current = get(i); 228 int next = current + delta; 229 if (compareAndSet(i, current, next)) 230 return next; 231 } 232 } 233 234 238 public String toString() { 239 if (array.length > 0) get(0); 241 return Arrays.toString(array); 242 } 243 244 } 245 | Popular Tags |