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

Print this page




 107      * if the current value {@code ==} the expected value.
 108      * @param expect the expected value
 109      * @param update the new value
 110      * @return true if successful. False return indicates that
 111      * the actual value was not equal to the expected value.
 112      */
 113     public final boolean compareAndSet(V expect, V update) {
 114         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
 115     }
 116 
 117     /**
 118      * Atomically sets the value to the given updated value
 119      * if the current value {@code ==} the expected value.
 120      *
 121      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 122      * and does not provide ordering guarantees, so is only rarely an
 123      * appropriate alternative to {@code compareAndSet}.
 124      *
 125      * @param expect the expected value
 126      * @param update the new value
 127      * @return true if successful.
 128      */
 129     public final boolean weakCompareAndSet(V expect, V update) {
 130         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
 131     }
 132 
 133     /**
 134      * Atomically sets to the given value and returns the old value.
 135      *
 136      * @param newValue the new value
 137      * @return the previous value
 138      */

 139     public final V getAndSet(V newValue) {
 140         while (true) {
 141             V x = get();
 142             if (compareAndSet(x, newValue))
 143                 return x;
 144         }
 145     }
 146 
 147     /**
 148      * Returns the String representation of the current value.
 149      * @return the String representation of the current value.
 150      */
 151     public String toString() {
 152         return String.valueOf(get());
 153     }
 154 
 155 }


 107      * if the current value {@code ==} the expected value.
 108      * @param expect the expected value
 109      * @param update the new value
 110      * @return true if successful. False return indicates that
 111      * the actual value was not equal to the expected value.
 112      */
 113     public final boolean compareAndSet(V expect, V update) {
 114         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
 115     }
 116 
 117     /**
 118      * Atomically sets the value to the given updated value
 119      * if the current value {@code ==} the expected value.
 120      *
 121      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 122      * and does not provide ordering guarantees, so is only rarely an
 123      * appropriate alternative to {@code compareAndSet}.
 124      *
 125      * @param expect the expected value
 126      * @param update the new value
 127      * @return true if successful
 128      */
 129     public final boolean weakCompareAndSet(V expect, V update) {
 130         return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
 131     }
 132 
 133     /**
 134      * Atomically sets to the given value and returns the old value.
 135      *
 136      * @param newValue the new value
 137      * @return the previous value
 138      */
 139     @SuppressWarnings("unchecked")
 140     public final V getAndSet(V newValue) {
 141         return (V)unsafe.getAndSetObject(this, valueOffset, newValue);



 142     }

 143 
 144     /**
 145      * Returns the String representation of the current value.
 146      * @return the String representation of the current value
 147      */
 148     public String toString() {
 149         return String.valueOf(get());
 150     }
 151 
 152 }