src/share/classes/java/util/concurrent/atomic/AtomicInteger.java

Print this page
rev 5834 : Rename compareAndSet methods, add updateAndGet/getAndUpdate to AtomicReference, add tests
Reviewed-by: briangoetz,smarks,mduigo
Contributed-by: Jim Gish <jim.gish@oracle.com>


 127      * Atomically sets the value to the given updated value
 128      * if the current value {@code ==} the expected value.
 129      *
 130      * @param expect the expected value
 131      * @param update the new value
 132      * @return true if successful. False return indicates that
 133      * the actual value was not equal to the expected value.
 134      */
 135     public final boolean compareAndSet(int expect, int update) {
 136         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
 137     }
 138 
 139     /**
 140      * Sets the value by applying the supplied operator to the old value,
 141      * and updating it atomically as in {@link #compareAndSet(int, int)}.
 142      * 
 143      * If the update results in a collision, we back off and retry the update 
 144      * (using the new current value) until successful. 
 145      * 
 146      * @param op the operator to use to compute the new value



















 147      * @return the new value
 148      */
 149     public final int compareAndSet(IntUnaryOperator op) {
 150         int oldValue, newValue;
 151         do {
 152             oldValue = get();
 153             newValue = op.operate(oldValue);
 154         } while (!compareAndSet(oldValue, newValue));
 155         return newValue;
 156     }
 157     
 158     /**
 159      * Atomically sets the value to the given updated value
 160      * if the current value {@code ==} the expected value.
 161      *
 162      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 163      * and does not provide ordering guarantees, so is only rarely an
 164      * appropriate alternative to {@code compareAndSet}.
 165      *
 166      * @param expect the expected value
 167      * @param update the new value
 168      * @return true if successful.
 169      */




 127      * Atomically sets the value to the given updated value
 128      * if the current value {@code ==} the expected value.
 129      *
 130      * @param expect the expected value
 131      * @param update the new value
 132      * @return true if successful. False return indicates that
 133      * the actual value was not equal to the expected value.
 134      */
 135     public final boolean compareAndSet(int expect, int update) {
 136         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
 137     }
 138 
 139     /**
 140      * Sets the value by applying the supplied operator to the old value,
 141      * and updating it atomically as in {@link #compareAndSet(int, int)}.
 142      * 
 143      * If the update results in a collision, we back off and retry the update 
 144      * (using the new current value) until successful. 
 145      * 
 146      * @param op the operator to use to compute the new value
 147      * @return the old value
 148      */
 149     public final int getAndUpdate(IntUnaryOperator op) {
 150         int oldValue, newValue;
 151         do {
 152             oldValue = get();
 153             newValue = op.operate(oldValue);
 154         } while (!compareAndSet(oldValue, newValue));
 155         return oldValue;
 156     }
 157 
 158     /**
 159      * Sets the value by applying the supplied operator to the old value,
 160      * and updating it atomically as in {@link #compareAndSet(int, int)}.
 161      * 
 162      * If the update results in a collision, we back off and retry the update 
 163      * (using the new current value) until successful. 
 164      * 
 165      * @param op the operator to use to compute the new value
 166      * @return the new value
 167      */
 168     public final int updateAndGet(IntUnaryOperator op) {
 169         int oldValue, newValue;
 170         do {
 171             oldValue = get();
 172             newValue = op.operate(oldValue);
 173         } while (!compareAndSet(oldValue, newValue));
 174         return newValue;
 175     }
 176     
 177     /**
 178      * Atomically sets the value to the given updated value
 179      * if the current value {@code ==} the expected value.
 180      *
 181      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 182      * and does not provide ordering guarantees, so is only rarely an
 183      * appropriate alternative to {@code compareAndSet}.
 184      *
 185      * @param expect the expected value
 186      * @param update the new value
 187      * @return true if successful.
 188      */