jdk/src/share/classes/java/util/concurrent/atomic/AtomicIntegerArray.java
changeset 15020 50394fa17c1b
parent 11134 9ff7640994bf
child 15267 c884f548a25f
equal deleted inserted replaced
15019:fb36233a54d9 15020:50394fa17c1b
   143      * @param i the index
   143      * @param i the index
   144      * @param newValue the new value
   144      * @param newValue the new value
   145      * @return the previous value
   145      * @return the previous value
   146      */
   146      */
   147     public final int getAndSet(int i, int newValue) {
   147     public final int getAndSet(int i, int newValue) {
   148         long offset = checkedByteOffset(i);
   148         return unsafe.getAndSetInt(array, checkedByteOffset(i), newValue);
   149         while (true) {
       
   150             int current = getRaw(offset);
       
   151             if (compareAndSetRaw(offset, current, newValue))
       
   152                 return current;
       
   153         }
       
   154     }
   149     }
   155 
   150 
   156     /**
   151     /**
   157      * Atomically sets the element at position {@code i} to the given
   152      * Atomically sets the element at position {@code i} to the given
   158      * updated value if the current value {@code ==} the expected value.
   153      * updated value if the current value {@code ==} the expected value.
   180      * appropriate alternative to {@code compareAndSet}.
   175      * appropriate alternative to {@code compareAndSet}.
   181      *
   176      *
   182      * @param i the index
   177      * @param i the index
   183      * @param expect the expected value
   178      * @param expect the expected value
   184      * @param update the new value
   179      * @param update the new value
   185      * @return true if successful.
   180      * @return true if successful
   186      */
   181      */
   187     public final boolean weakCompareAndSet(int i, int expect, int update) {
   182     public final boolean weakCompareAndSet(int i, int expect, int update) {
   188         return compareAndSet(i, expect, update);
   183         return compareAndSet(i, expect, update);
   189     }
   184     }
   190 
   185 
   214      * @param i the index
   209      * @param i the index
   215      * @param delta the value to add
   210      * @param delta the value to add
   216      * @return the previous value
   211      * @return the previous value
   217      */
   212      */
   218     public final int getAndAdd(int i, int delta) {
   213     public final int getAndAdd(int i, int delta) {
   219         long offset = checkedByteOffset(i);
   214         return unsafe.getAndAddInt(array, checkedByteOffset(i), delta);
   220         while (true) {
       
   221             int current = getRaw(offset);
       
   222             if (compareAndSetRaw(offset, current, current + delta))
       
   223                 return current;
       
   224         }
       
   225     }
   215     }
   226 
   216 
   227     /**
   217     /**
   228      * Atomically increments by one the element at index {@code i}.
   218      * Atomically increments by one the element at index {@code i}.
   229      *
   219      *
   230      * @param i the index
   220      * @param i the index
   231      * @return the updated value
   221      * @return the updated value
   232      */
   222      */
   233     public final int incrementAndGet(int i) {
   223     public final int incrementAndGet(int i) {
   234         return addAndGet(i, 1);
   224         return getAndAdd(i, 1) + 1;
   235     }
   225     }
   236 
   226 
   237     /**
   227     /**
   238      * Atomically decrements by one the element at index {@code i}.
   228      * Atomically decrements by one the element at index {@code i}.
   239      *
   229      *
   240      * @param i the index
   230      * @param i the index
   241      * @return the updated value
   231      * @return the updated value
   242      */
   232      */
   243     public final int decrementAndGet(int i) {
   233     public final int decrementAndGet(int i) {
   244         return addAndGet(i, -1);
   234         return getAndAdd(i, -1) - 1;
   245     }
   235     }
   246 
   236 
   247     /**
   237     /**
   248      * Atomically adds the given value to the element at index {@code i}.
   238      * Atomically adds the given value to the element at index {@code i}.
   249      *
   239      *
   250      * @param i the index
   240      * @param i the index
   251      * @param delta the value to add
   241      * @param delta the value to add
   252      * @return the updated value
   242      * @return the updated value
   253      */
   243      */
   254     public final int addAndGet(int i, int delta) {
   244     public final int addAndGet(int i, int delta) {
   255         long offset = checkedByteOffset(i);
   245         return getAndAdd(i, delta) + delta;
   256         while (true) {
       
   257             int current = getRaw(offset);
       
   258             int next = current + delta;
       
   259             if (compareAndSetRaw(offset, current, next))
       
   260                 return next;
       
   261         }
       
   262     }
   246     }
   263 
   247 
   264     /**
   248     /**
   265      * Returns the String representation of the current values of array.
   249      * Returns the String representation of the current values of array.
   266      * @return the String representation of the current values of array
   250      * @return the String representation of the current values of array