1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 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 * An {@code int} 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 AtomicInteger} is used in applications such as atomically 44 * incremented counters, and cannot be used as a replacement for an 45 * {@link java.lang.Integer}. 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 AtomicInteger extends Number implements java.io.Serializable { 53 private static final long serialVersionUID = 6214790243416807050L; 54 55 // setup to use Unsafe.compareAndSwapInt for updates 56 private static final Unsafe unsafe = Unsafe.getUnsafe(); 57 private static final long valueOffset; 58 59 static { 60 try { 61 valueOffset = unsafe.objectFieldOffset 62 (AtomicInteger.class.getDeclaredField("value")); 63 } catch (Exception ex) { throw new Error(ex); } 64 } 65 66 private volatile int value; 67 68 /** 69 * Creates a new AtomicInteger with the given initial value. 70 * 71 * @param initialValue the initial value 72 */ 73 public AtomicInteger(int initialValue) { 74 value = initialValue; 75 } 76 77 /** 78 * Creates a new AtomicInteger with initial value {@code 0}. 79 */ 80 public AtomicInteger() { 81 } 82 83 /** 84 * Gets the current value. 85 * 86 * @return the current value 87 */ 88 public final int get() { 89 return value; 90 } 91 92 /** 93 * Sets to the given value. 94 * 95 * @param newValue the new value 96 */ 97 public final void set(int newValue) { 98 value = newValue; 99 } 100 101 /** 102 * Eventually sets to the given value. 103 * 104 * @param newValue the new value 105 * @since 1.6 106 */ 107 public final void lazySet(int newValue) { 108 unsafe.putOrderedInt(this, valueOffset, newValue); 109 } 110 111 /** 112 * Atomically sets to the given value and returns the old value. 113 * 114 * @param newValue the new value 115 * @return the previous value 116 */ 117 public final int getAndSet(int newValue) { 118 return unsafe.getAndSetInt(this, valueOffset, newValue); 119 } 120 121 /** 122 * Atomically sets the value to the given updated value 123 * if the current value {@code ==} the expected value. 124 * 125 * @param expect the expected value 126 * @param update the new value 127 * @return true if successful. False return indicates that 128 * the actual value was not equal to the expected value. 129 */ 130 public final boolean compareAndSet(int expect, int update) { 131 return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 132 } 133 134 /** 135 * Atomically sets the value to the given updated value 136 * if the current value {@code ==} the expected value. 137 * 138 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 139 * and does not provide ordering guarantees, so is only rarely an 140 * appropriate alternative to {@code compareAndSet}. 141 * 142 * @param expect the expected value 143 * @param update the new value 144 * @return true if successful 145 */ 146 public final boolean weakCompareAndSet(int expect, int update) { 147 return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 148 } 149 150 /** 151 * Atomically increments by one the current value. 152 * 153 * @return the previous value 154 */ 155 public final int getAndIncrement() { 156 return getAndAdd(1); 157 } 158 159 /** 160 * Atomically decrements by one the current value. 161 * 162 * @return the previous value 163 */ 164 public final int getAndDecrement() { 165 return getAndAdd(-1); 166 } 167 168 /** 169 * Atomically adds the given value to the current value. 170 * 171 * @param delta the value to add 172 * @return the previous value 173 */ 174 public final int getAndAdd(int delta) { 175 return unsafe.getAndAddInt(this, valueOffset, delta); 176 } 177 178 /** 179 * Atomically increments by one the current value. 180 * 181 * @return the updated value 182 */ 183 public final int incrementAndGet() { 184 return getAndAdd(1) + 1; 185 } 186 187 /** 188 * Atomically decrements by one the current value. 189 * 190 * @return the updated value 191 */ 192 public final int decrementAndGet() { 193 return getAndAdd(-1) - 1; 194 } 195 196 /** 197 * Atomically adds the given value to the current value. 198 * 199 * @param delta the value to add 200 * @return the updated value 201 */ 202 public final int addAndGet(int delta) { 203 return getAndAdd(delta) + delta; 204 } 205 206 /** 207 * Returns the String representation of the current value. 208 * @return the String representation of the current value 209 */ 210 public String toString() { 211 return Integer.toString(get()); 212 } 213 214 /** 215 * Returns the value of this {@code AtomicInteger} as an {@code int}. 216 */ 217 public int intValue() { 218 return get(); 219 } 220 221 /** 222 * Returns the value of this {@code AtomicInteger} as a {@code long} 223 * after a widening primitive conversion. 224 * @jls 5.1.2 Widening Primitive Conversions 225 */ 226 public long longValue() { 227 return (long)get(); 228 } 229 230 /** 231 * Returns the value of this {@code AtomicInteger} as a {@code float} 232 * after a widening primitive conversion. 233 * @jls 5.1.2 Widening Primitive Conversions 234 */ 235 public float floatValue() { 236 return (float)get(); 237 } 238 239 /** 240 * Returns the value of this {@code AtomicInteger} as a {@code double} 241 * after a widening primitive conversion. 242 * @jls 5.1.2 Widening Primitive Conversions 243 */ 244 public double doubleValue() { 245 return (double)get(); 246 } 247 248 }