1 7 8 package java.util.concurrent.atomic; 9 import sun.misc.Unsafe; 10 import java.util.*; 11 12 19 public class AtomicLongArray implements java.io.Serializable { 20 private static final long serialVersionUID = -2308431214976778248L; 21 22 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 ("index " + i); 31 return base + i * scale; 32 } 33 34 38 public AtomicLongArray(int length) { 39 array = new long[length]; 40 if (length > 0) 42 unsafe.putLongVolatile(array, rawIndex(0), 0); 43 } 44 45 52 public AtomicLongArray(long[] array) { 53 if (array == null) 54 throw new NullPointerException (); 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 unsafe.putLongVolatile(this.array, rawIndex(last), array[last]); 63 } 64 } 65 66 71 public final int length() { 72 return array.length; 73 } 74 75 81 public final long get(int i) { 82 return unsafe.getLongVolatile(array, rawIndex(i)); 83 } 84 85 91 public final void set(int i, long newValue) { 92 unsafe.putLongVolatile(array, rawIndex(i), newValue); 93 } 94 95 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 120 public final boolean compareAndSet(int i, long expect, long update) { 121 return unsafe.compareAndSwapLong(array, rawIndex(i), 122 expect, update); 123 } 124 125 134 public final boolean weakCompareAndSet(int i, long expect, long update) { 135 return compareAndSet(i, expect, update); 136 } 137 138 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 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 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 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 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 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 235 public String toString() { 236 if (array.length > 0) get(0); 238 return Arrays.toString(array); 239 } 240 241 } 242 | Popular Tags |