jtulach@1334: /* jtulach@1334: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1334: * jtulach@1334: * This code is free software; you can redistribute it and/or modify it jtulach@1334: * under the terms of the GNU General Public License version 2 only, as jtulach@1334: * published by the Free Software Foundation. Oracle designates this jtulach@1334: * particular file as subject to the "Classpath" exception as provided jtulach@1334: * by Oracle in the LICENSE file that accompanied this code. jtulach@1334: * jtulach@1334: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1334: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1334: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1334: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1334: * accompanied this code). jtulach@1334: * jtulach@1334: * You should have received a copy of the GNU General Public License version jtulach@1334: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1334: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1334: * jtulach@1334: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1334: * or visit www.oracle.com if you need additional information or have any jtulach@1334: * questions. jtulach@1334: */ jtulach@1334: jtulach@1334: /* jtulach@1334: * This file is available under and governed by the GNU General Public jtulach@1334: * License version 2 only, as published by the Free Software Foundation. jtulach@1334: * However, the following notice accompanied the original version of this jtulach@1334: * file: jtulach@1334: * jtulach@1334: * Written by Doug Lea with assistance from members of JCP JSR-166 jtulach@1334: * Expert Group and released to the public domain, as explained at jtulach@1334: * http://creativecommons.org/publicdomain/zero/1.0/ jtulach@1334: */ jtulach@1334: jtulach@1334: package java.util.concurrent.atomic; jtulach@1334: jtulach@1334: /** jtulach@1334: * An {@code int} value that may be updated atomically. See the jtulach@1334: * {@link java.util.concurrent.atomic} package specification for jtulach@1334: * description of the properties of atomic variables. An jtulach@1334: * {@code AtomicInteger} is used in applications such as atomically jtulach@1334: * incremented counters, and cannot be used as a replacement for an jtulach@1334: * {@link java.lang.Integer}. However, this class does extend jtulach@1334: * {@code Number} to allow uniform access by tools and utilities that jtulach@1334: * deal with numerically-based classes. jtulach@1334: * jtulach@1334: * @since 1.5 jtulach@1334: * @author Doug Lea jtulach@1334: */ jtulach@1334: public class AtomicInteger extends Number implements java.io.Serializable { jtulach@1334: private static final long serialVersionUID = 6214790243416807050L; jtulach@1334: jtulach@1334: private volatile int value; jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new AtomicInteger with the given initial value. jtulach@1334: * jtulach@1334: * @param initialValue the initial value jtulach@1334: */ jtulach@1334: public AtomicInteger(int initialValue) { jtulach@1334: value = initialValue; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new AtomicInteger with initial value {@code 0}. jtulach@1334: */ jtulach@1334: public AtomicInteger() { jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the current value. jtulach@1334: * jtulach@1334: * @return the current value jtulach@1334: */ jtulach@1334: public final int get() { jtulach@1334: return value; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Sets to the given value. jtulach@1334: * jtulach@1334: * @param newValue the new value jtulach@1334: */ jtulach@1334: public final void set(int newValue) { jtulach@1334: value = newValue; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Eventually sets to the given value. jtulach@1334: * jtulach@1334: * @param newValue the new value jtulach@1334: * @since 1.6 jtulach@1334: */ jtulach@1334: public final void lazySet(int newValue) { jaroslav@1338: value = newValue; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets to the given value and returns the old value. jtulach@1334: * jtulach@1334: * @param newValue the new value jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndSet(int newValue) { jtulach@1334: for (;;) { jtulach@1334: int current = get(); jtulach@1334: if (compareAndSet(current, newValue)) jtulach@1334: return current; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the value to the given updated value jtulach@1334: * if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. False return indicates that jtulach@1334: * the actual value was not equal to the expected value. jtulach@1334: */ jtulach@1334: public final boolean compareAndSet(int expect, int update) { jaroslav@1338: if (value == expect) { jaroslav@1338: value = update; jaroslav@1338: return true; jaroslav@1338: } else { jaroslav@1338: return false; jaroslav@1338: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the value to the given updated value jtulach@1334: * if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: *

May fail spuriously jtulach@1334: * and does not provide ordering guarantees, so is only rarely an jtulach@1334: * appropriate alternative to {@code compareAndSet}. jtulach@1334: * jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. jtulach@1334: */ jtulach@1334: public final boolean weakCompareAndSet(int expect, int update) { jaroslav@1338: return compareAndSet(expect, update); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically increments by one the current value. jtulach@1334: * jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndIncrement() { jtulach@1334: for (;;) { jtulach@1334: int current = get(); jtulach@1334: int next = current + 1; jtulach@1334: if (compareAndSet(current, next)) jtulach@1334: return current; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically decrements by one the current value. jtulach@1334: * jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndDecrement() { jtulach@1334: for (;;) { jtulach@1334: int current = get(); jtulach@1334: int next = current - 1; jtulach@1334: if (compareAndSet(current, next)) jtulach@1334: return current; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically adds the given value to the current value. jtulach@1334: * jtulach@1334: * @param delta the value to add jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final int getAndAdd(int delta) { jtulach@1334: for (;;) { jtulach@1334: int current = get(); jtulach@1334: int next = current + delta; jtulach@1334: if (compareAndSet(current, next)) jtulach@1334: return current; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically increments by one the current value. jtulach@1334: * jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final int incrementAndGet() { jtulach@1334: for (;;) { jtulach@1334: int current = get(); jtulach@1334: int next = current + 1; jtulach@1334: if (compareAndSet(current, next)) jtulach@1334: return next; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically decrements by one the current value. jtulach@1334: * jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final int decrementAndGet() { jtulach@1334: for (;;) { jtulach@1334: int current = get(); jtulach@1334: int next = current - 1; jtulach@1334: if (compareAndSet(current, next)) jtulach@1334: return next; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically adds the given value to the current value. jtulach@1334: * jtulach@1334: * @param delta the value to add jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final int addAndGet(int delta) { jtulach@1334: for (;;) { jtulach@1334: int current = get(); jtulach@1334: int next = current + delta; jtulach@1334: if (compareAndSet(current, next)) jtulach@1334: return next; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the String representation of the current value. jtulach@1334: * @return the String representation of the current value. jtulach@1334: */ jtulach@1334: public String toString() { jtulach@1334: return Integer.toString(get()); jtulach@1334: } jtulach@1334: jtulach@1334: jtulach@1334: public int intValue() { jtulach@1334: return get(); jtulach@1334: } jtulach@1334: jtulach@1334: public long longValue() { jtulach@1334: return (long)get(); jtulach@1334: } jtulach@1334: jtulach@1334: public float floatValue() { jtulach@1334: return (float)get(); jtulach@1334: } jtulach@1334: jtulach@1334: public double doubleValue() { jtulach@1334: return (double)get(); jtulach@1334: } jtulach@1334: jtulach@1334: }