17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * Written by Doug Lea with assistance from members of JCP JSR-166 32 * Expert Group and released to the public domain, as explained at 33 * http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util.concurrent.atomic; 37 import sun.misc.Unsafe; 38 39 /** 40 * An {@code int} array in which elements may be updated atomically. 41 * See the {@link java.util.concurrent.atomic} package 42 * specification for description of the properties of atomic 43 * variables. 44 * @since 1.5 45 * @author Doug Lea 46 */ 47 public class AtomicIntegerArray implements java.io.Serializable { 48 private static final long serialVersionUID = 2862133569453604235L; 49 50 private static final Unsafe unsafe = Unsafe.getUnsafe(); 51 private static final int base = unsafe.arrayBaseOffset(int[].class); 52 private static final int shift; 53 private final int[] array; 54 55 static { 56 int scale = unsafe.arrayIndexScale(int[].class); 228 * Atomically decrements by one the element at index {@code i}. 229 * 230 * @param i the index 231 * @return the updated value 232 */ 233 public final int decrementAndGet(int i) { 234 return getAndAdd(i, -1) - 1; 235 } 236 237 /** 238 * Atomically adds the given value to the element at index {@code i}. 239 * 240 * @param i the index 241 * @param delta the value to add 242 * @return the updated value 243 */ 244 public final int addAndGet(int i, int delta) { 245 return getAndAdd(i, delta) + delta; 246 } 247 248 /** 249 * Returns the String representation of the current values of array. 250 * @return the String representation of the current values of array 251 */ 252 public String toString() { 253 int iMax = array.length - 1; 254 if (iMax == -1) 255 return "[]"; 256 257 StringBuilder b = new StringBuilder(); 258 b.append('['); 259 for (int i = 0; ; i++) { 260 b.append(getRaw(byteOffset(i))); 261 if (i == iMax) 262 return b.append(']').toString(); 263 b.append(',').append(' '); 264 } 265 } 266 267 } | 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * Written by Doug Lea with assistance from members of JCP JSR-166 32 * Expert Group and released to the public domain, as explained at 33 * http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util.concurrent.atomic; 37 import java.util.function.IntUnaryOperator; 38 import java.util.function.IntBinaryOperator; 39 import sun.misc.Unsafe; 40 41 /** 42 * An {@code int} array in which elements may be updated atomically. 43 * See the {@link java.util.concurrent.atomic} package 44 * specification for description of the properties of atomic 45 * variables. 46 * @since 1.5 47 * @author Doug Lea 48 */ 49 public class AtomicIntegerArray implements java.io.Serializable { 50 private static final long serialVersionUID = 2862133569453604235L; 51 52 private static final Unsafe unsafe = Unsafe.getUnsafe(); 53 private static final int base = unsafe.arrayBaseOffset(int[].class); 54 private static final int shift; 55 private final int[] array; 56 57 static { 58 int scale = unsafe.arrayIndexScale(int[].class); 230 * Atomically decrements by one the element at index {@code i}. 231 * 232 * @param i the index 233 * @return the updated value 234 */ 235 public final int decrementAndGet(int i) { 236 return getAndAdd(i, -1) - 1; 237 } 238 239 /** 240 * Atomically adds the given value to the element at index {@code i}. 241 * 242 * @param i the index 243 * @param delta the value to add 244 * @return the updated value 245 */ 246 public final int addAndGet(int i, int delta) { 247 return getAndAdd(i, delta) + delta; 248 } 249 250 /** 251 * Atomically updates the element at index {@code i} with the results 252 * of applying the given function. The function should be 253 * side-effect-free, since it may be re-applied when attempted 254 * updates fail due to contention among threads. 255 * 256 * @param i the index 257 * @param updateFunction a side-effect-free function 258 * @return the previous value 259 * @since 1.8 260 */ 261 public final int getAndUpdate(int i, IntUnaryOperator updateFunction) { 262 long offset = checkedByteOffset(i); 263 int prev, next; 264 do { 265 prev = getRaw(offset); 266 next = updateFunction.operateAsInt(prev); 267 } while (!compareAndSetRaw(offset, prev, next)); 268 return prev; 269 } 270 271 /** 272 * Atomically updates the element at index {@code i} with the results 273 * of applying the given function. The function should be 274 * side-effect-free, since it may be re-applied when attempted 275 * updates fail due to contention among threads. 276 * 277 * @param i the index 278 * @param updateFunction a side-effect-free function 279 * @return the updated value 280 * @since 1.8 281 */ 282 public final int updateAndGet(int i, IntUnaryOperator updateFunction) { 283 long offset = checkedByteOffset(i); 284 int prev, next; 285 do { 286 prev = getRaw(offset); 287 next = updateFunction.operateAsInt(prev); 288 } while (!compareAndSetRaw(offset, prev, next)); 289 return next; 290 } 291 292 /** 293 * Atomically updates the element at index {@code i} with the results 294 * of applying the given function to the current and given values. 295 * The function should be side-effect-free, since it may be 296 * re-applied when attempted updates fail due to contention among 297 * threads. The function is applied with the current value at index 298 * {@code i} as its first argument, and the given update as the second 299 * argument. 300 * 301 * @param i the index 302 * @param x the update value 303 * @param accumulatorFunction a side-effect-free function of two arguments 304 * @return the previous value 305 * @since 1.8 306 */ 307 public final int getAndAccumulate(int i, int x, 308 IntBinaryOperator accumulatorFunction) { 309 long offset = checkedByteOffset(i); 310 int prev, next; 311 do { 312 prev = getRaw(offset); 313 next = accumulatorFunction.operateAsInt(prev, x); 314 } while (!compareAndSetRaw(offset, prev, next)); 315 return prev; 316 } 317 318 /** 319 * Atomically updates the element at index {@code i} with the results 320 * of applying the given function to the current and given values. 321 * The function should be side-effect-free, since it may be 322 * re-applied when attempted updates fail due to contention among 323 * threads. The function is applied with the current value at index 324 * {@code i} as its first argument, and the given update as the second 325 * argument. 326 * 327 * @param i the index 328 * @param x the update value 329 * @param accumulatorFunction a side-effect-free function of two arguments 330 * @return the updated value 331 * @since 1.8 332 */ 333 public final int accumulateAndGet(int i, int x, 334 IntBinaryOperator accumulatorFunction) { 335 long offset = checkedByteOffset(i); 336 int prev, next; 337 do { 338 prev = getRaw(offset); 339 next = accumulatorFunction.operateAsInt(prev, x); 340 } while (!compareAndSetRaw(offset, prev, next)); 341 return next; 342 } 343 344 /** 345 * Returns the String representation of the current values of array. 346 * @return the String representation of the current values of array 347 */ 348 public String toString() { 349 int iMax = array.length - 1; 350 if (iMax == -1) 351 return "[]"; 352 353 StringBuilder b = new StringBuilder(); 354 b.append('['); 355 for (int i = 0; ; i++) { 356 b.append(getRaw(byteOffset(i))); 357 if (i == iMax) 358 return b.append(']').toString(); 359 b.append(',').append(' '); 360 } 361 } 362 363 } |