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

Print this page




  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  * A {@code long} value that may be updated atomically.  See the
  41  * {@link java.util.concurrent.atomic} package specification for
  42  * description of the properties of atomic variables. An
  43  * {@code AtomicLong} is used in applications such as atomically
  44  * incremented sequence numbers, and cannot be used as a replacement
  45  * for a {@link java.lang.Long}. However, this class does extend
  46  * {@code Number} to allow uniform access by tools and utilities that
  47  * deal with numerically-based classes.
  48  *
  49  * @since 1.5
  50  * @author Doug Lea
  51  */
  52 public class AtomicLong extends Number implements java.io.Serializable {
  53     private static final long serialVersionUID = 1927816293512124184L;
  54 
  55     // setup to use Unsafe.compareAndSwapLong for updates
  56     private static final Unsafe unsafe = Unsafe.getUnsafe();


 200 
 201     /**
 202      * Atomically decrements by one the current value.
 203      *
 204      * @return the updated value
 205      */
 206     public final long decrementAndGet() {
 207         return getAndAdd(-1) - 1;
 208     }
 209 
 210     /**
 211      * Atomically adds the given value to the current value.
 212      *
 213      * @param delta the value to add
 214      * @return the updated value
 215      */
 216     public final long addAndGet(long delta) {
 217         return getAndAdd(delta) + delta;
 218     }
 219 




















































































 220     /**
 221      * Returns the String representation of the current value.
 222      * @return the String representation of the current value
 223      */
 224     public String toString() {
 225         return Long.toString(get());
 226     }
 227 
 228     /**
 229      * Returns the value of this {@code AtomicLong} as an {@code int}
 230      * after a narrowing primitive conversion.
 231      * @jls 5.1.3 Narrowing Primitive Conversions
 232      */
 233     public int intValue() {
 234         return (int)get();
 235     }
 236 
 237     /**
 238      * Returns the value of this {@code AtomicLong} as a {@code long}.
 239      */




  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.LongUnaryOperator;
  38 import java.util.function.LongBinaryOperator;
  39 import sun.misc.Unsafe;
  40 
  41 /**
  42  * A {@code long} value that may be updated atomically.  See the
  43  * {@link java.util.concurrent.atomic} package specification for
  44  * description of the properties of atomic variables. An
  45  * {@code AtomicLong} is used in applications such as atomically
  46  * incremented sequence numbers, and cannot be used as a replacement
  47  * for a {@link java.lang.Long}. However, this class does extend
  48  * {@code Number} to allow uniform access by tools and utilities that
  49  * deal with numerically-based classes.
  50  *
  51  * @since 1.5
  52  * @author Doug Lea
  53  */
  54 public class AtomicLong extends Number implements java.io.Serializable {
  55     private static final long serialVersionUID = 1927816293512124184L;
  56 
  57     // setup to use Unsafe.compareAndSwapLong for updates
  58     private static final Unsafe unsafe = Unsafe.getUnsafe();


 202 
 203     /**
 204      * Atomically decrements by one the current value.
 205      *
 206      * @return the updated value
 207      */
 208     public final long decrementAndGet() {
 209         return getAndAdd(-1) - 1;
 210     }
 211 
 212     /**
 213      * Atomically adds the given value to the current value.
 214      *
 215      * @param delta the value to add
 216      * @return the updated value
 217      */
 218     public final long addAndGet(long delta) {
 219         return getAndAdd(delta) + delta;
 220     }
 221 
 222     /**
 223      * Atomically updates the current value with the results of
 224      * applying the given function. The function should be
 225      * side-effect-free, since it may be re-applied when attempted
 226      * updates fail due to contention among threads.
 227      *
 228      * @param updateFunction a side-effect-free function
 229      * @return the previous value
 230      * @since 1.8
 231      */
 232     public final long getAndUpdate(LongUnaryOperator updateFunction) {
 233         long prev, next;
 234         do {
 235             prev = get();
 236             next = updateFunction.operateAsLong(prev);
 237         } while (!compareAndSet(prev, next));
 238         return prev;
 239     }
 240 
 241     /**
 242      * Atomically updates the current value with the results of
 243      * applying the given function. The function should be
 244      * side-effect-free, since it may be re-applied when attempted
 245      * updates fail due to contention among threads.
 246      *
 247      * @param updateFunction a side-effect-free function
 248      * @return the updated value
 249      * @since 1.8
 250      */
 251     public final long updateAndGet(LongUnaryOperator updateFunction) {
 252         long prev, next;
 253         do {
 254             prev = get();
 255             next = updateFunction.operateAsLong(prev);
 256         } while (!compareAndSet(prev, next));
 257         return next;
 258     }
 259 
 260     /**
 261      * Atomically updates the current value with the results of
 262      * applying the given function to the current and given values.
 263      * The function should be side-effect-free, since it may be
 264      * re-applied when attempted updates fail due to contention among
 265      * threads.  The function is applied with the current value as its
 266      * first argument, and the given update as the second argument.
 267      *
 268      * @param x the update value
 269      * @param accumulatorFunction a side-effect-free function of two arguments
 270      * @return the previous value
 271      * @since 1.8
 272      */
 273     public final long getAndAccumulate(long x,
 274                                        LongBinaryOperator accumulatorFunction) {
 275         long prev, next;
 276         do {
 277             prev = get();
 278             next = accumulatorFunction.operateAsLong(prev, x);
 279         } while (!compareAndSet(prev, next));
 280         return prev;
 281     }
 282 
 283     /**
 284      * Atomically updates the current value with the results of
 285      * applying the given function to the current and given values.
 286      * The function should be side-effect-free, since it may be
 287      * re-applied when attempted updates fail due to contention among
 288      * threads.  The function is applied with the current value as its
 289      * first argument, and the given update as the second argument.
 290      *
 291      * @param x the update value
 292      * @param accumulatorFunction a side-effect-free function of two arguments
 293      * @return the updated value
 294      * @since 1.8
 295      */
 296     public final long accumulateAndGet(long x,
 297                                        LongBinaryOperator accumulatorFunction) {
 298         long prev, next;
 299         do {
 300             prev = get();
 301             next = accumulatorFunction.operateAsLong(prev, x);
 302         } while (!compareAndSet(prev, next));
 303         return next;
 304     }
 305 
 306     /**
 307      * Returns the String representation of the current value.
 308      * @return the String representation of the current value
 309      */
 310     public String toString() {
 311         return Long.toString(get());
 312     }
 313 
 314     /**
 315      * Returns the value of this {@code AtomicLong} as an {@code int}
 316      * after a narrowing primitive conversion.
 317      * @jls 5.1.3 Narrowing Primitive Conversions
 318      */
 319     public int intValue() {
 320         return (int)get();
 321     }
 322 
 323     /**
 324      * Returns the value of this {@code AtomicLong} as a {@code long}.
 325      */