jdk/src/share/classes/java/util/concurrent/atomic/AtomicReference.java
changeset 15020 50394fa17c1b
parent 14325 622c473a21aa
child 15267 c884f548a25f
equal deleted inserted replaced
15019:fb36233a54d9 15020:50394fa17c1b
   122      * and does not provide ordering guarantees, so is only rarely an
   122      * and does not provide ordering guarantees, so is only rarely an
   123      * appropriate alternative to {@code compareAndSet}.
   123      * appropriate alternative to {@code compareAndSet}.
   124      *
   124      *
   125      * @param expect the expected value
   125      * @param expect the expected value
   126      * @param update the new value
   126      * @param update the new value
   127      * @return true if successful.
   127      * @return true if successful
   128      */
   128      */
   129     public final boolean weakCompareAndSet(V expect, V update) {
   129     public final boolean weakCompareAndSet(V expect, V update) {
   130         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
   130         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
   131     }
   131     }
   132 
   132 
   134      * Atomically sets to the given value and returns the old value.
   134      * Atomically sets to the given value and returns the old value.
   135      *
   135      *
   136      * @param newValue the new value
   136      * @param newValue the new value
   137      * @return the previous value
   137      * @return the previous value
   138      */
   138      */
       
   139     @SuppressWarnings("unchecked")
   139     public final V getAndSet(V newValue) {
   140     public final V getAndSet(V newValue) {
   140         while (true) {
   141         return (V)unsafe.getAndSetObject(this, valueOffset, newValue);
   141             V x = get();
       
   142             if (compareAndSet(x, newValue))
       
   143                 return x;
       
   144         }
       
   145     }
   142     }
   146 
   143 
   147     /**
   144     /**
   148      * Returns the String representation of the current value.
   145      * Returns the String representation of the current value.
   149      * @return the String representation of the current value.
   146      * @return the String representation of the current value
   150      */
   147      */
   151     public String toString() {
   148     public String toString() {
   152         return String.valueOf(get());
   149         return String.valueOf(get());
   153     }
   150     }
   154 
   151