0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/Apackage java.util.concurrent.atomic;
0N/Aimport sun.misc.Unsafe;
0N/A
0N/A/**
0N/A * An {@code int} value that may be updated atomically. See the
0N/A * {@link java.util.concurrent.atomic} package specification for
0N/A * description of the properties of atomic variables. An
0N/A * {@code AtomicInteger} is used in applications such as atomically
0N/A * incremented counters, and cannot be used as a replacement for an
0N/A * {@link java.lang.Integer}. However, this class does extend
0N/A * {@code Number} to allow uniform access by tools and utilities that
0N/A * deal with numerically-based classes.
0N/A *
0N/A * @since 1.5
0N/A * @author Doug Lea
0N/A*/
0N/Apublic class AtomicInteger extends Number implements java.io.Serializable {
0N/A private static final long serialVersionUID = 6214790243416807050L;
0N/A
0N/A // setup to use Unsafe.compareAndSwapInt for updates
0N/A private static final Unsafe unsafe = Unsafe.getUnsafe();
0N/A private static final long valueOffset;
0N/A
0N/A static {
0N/A try {
0N/A valueOffset = unsafe.objectFieldOffset
0N/A (AtomicInteger.class.getDeclaredField("value"));
0N/A } catch (Exception ex) { throw new Error(ex); }
0N/A }
0N/A
0N/A private volatile int value;
0N/A
0N/A /**
0N/A * Creates a new AtomicInteger with the given initial value.
0N/A *
0N/A * @param initialValue the initial value
0N/A */
0N/A public AtomicInteger(int initialValue) {
0N/A value = initialValue;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new AtomicInteger with initial value {@code 0}.
0N/A */
0N/A public AtomicInteger() {
0N/A }
0N/A
0N/A /**
0N/A * Gets the current value.
0N/A *
0N/A * @return the current value
0N/A */
0N/A public final int get() {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Sets to the given value.
0N/A *
0N/A * @param newValue the new value
0N/A */
0N/A public final void set(int newValue) {
0N/A value = newValue;
0N/A }
0N/A
0N/A /**
0N/A * Eventually sets to the given value.
0N/A *
0N/A * @param newValue the new value
0N/A * @since 1.6
0N/A */
0N/A public final void lazySet(int newValue) {
0N/A unsafe.putOrderedInt(this, valueOffset, newValue);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets to the given value and returns the old value.
0N/A *
0N/A * @param newValue the new value
0N/A * @return the previous value
0N/A */
0N/A public final int getAndSet(int newValue) {
0N/A for (;;) {
0N/A int current = get();
0N/A if (compareAndSet(current, newValue))
0N/A return current;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the value to the given updated value
0N/A * if the current value {@code ==} the expected value.
0N/A *
0N/A * @param expect the expected value
0N/A * @param update the new value
0N/A * @return true if successful. False return indicates that
0N/A * the actual value was not equal to the expected value.
0N/A */
0N/A public final boolean compareAndSet(int expect, int update) {
0N/A return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the value to the given updated value
0N/A * if the current value {@code ==} the expected value.
0N/A *
0N/A * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
0N/A * and does not provide ordering guarantees, so is only rarely an
0N/A * appropriate alternative to {@code compareAndSet}.
0N/A *
0N/A * @param expect the expected value
0N/A * @param update the new value
0N/A * @return true if successful.
0N/A */
0N/A public final boolean weakCompareAndSet(int expect, int update) {
0N/A return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
0N/A }
0N/A
0N/A /**
0N/A * Atomically increments by one the current value.
0N/A *
0N/A * @return the previous value
0N/A */
0N/A public final int getAndIncrement() {
0N/A for (;;) {
0N/A int current = get();
0N/A int next = current + 1;
0N/A if (compareAndSet(current, next))
0N/A return current;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Atomically decrements by one the current value.
0N/A *
0N/A * @return the previous value
0N/A */
0N/A public final int getAndDecrement() {
0N/A for (;;) {
0N/A int current = get();
0N/A int next = current - 1;
0N/A if (compareAndSet(current, next))
0N/A return current;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Atomically adds the given value to the current value.
0N/A *
0N/A * @param delta the value to add
0N/A * @return the previous value
0N/A */
0N/A public final int getAndAdd(int delta) {
0N/A for (;;) {
0N/A int current = get();
0N/A int next = current + delta;
0N/A if (compareAndSet(current, next))
0N/A return current;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Atomically increments by one the current value.
0N/A *
0N/A * @return the updated value
0N/A */
0N/A public final int incrementAndGet() {
0N/A for (;;) {
0N/A int current = get();
0N/A int next = current + 1;
0N/A if (compareAndSet(current, next))
0N/A return next;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Atomically decrements by one the current value.
0N/A *
0N/A * @return the updated value
0N/A */
0N/A public final int decrementAndGet() {
0N/A for (;;) {
0N/A int current = get();
0N/A int next = current - 1;
0N/A if (compareAndSet(current, next))
0N/A return next;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Atomically adds the given value to the current value.
0N/A *
0N/A * @param delta the value to add
0N/A * @return the updated value
0N/A */
0N/A public final int addAndGet(int delta) {
0N/A for (;;) {
0N/A int current = get();
0N/A int next = current + delta;
0N/A if (compareAndSet(current, next))
0N/A return next;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the String representation of the current value.
0N/A * @return the String representation of the current value.
0N/A */
0N/A public String toString() {
0N/A return Integer.toString(get());
0N/A }
0N/A
0N/A
0N/A public int intValue() {
0N/A return get();
0N/A }
0N/A
0N/A public long longValue() {
0N/A return (long)get();
0N/A }
0N/A
0N/A public float floatValue() {
0N/A return (float)get();
0N/A }
0N/A
0N/A public double doubleValue() {
0N/A return (double)get();
0N/A }
0N/A
0N/A}