2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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
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/
36 package java.util.concurrent.atomic;
39 * A {@code long} array in which elements may be updated atomically.
40 * See the {@link java.util.concurrent.atomic} package specification
41 * for description of the properties of atomic variables.
45 public class AtomicLongArray implements java.io.Serializable {
46 private static final long serialVersionUID = -2308431214976778248L;
48 private final long[] array;
51 * Creates a new AtomicLongArray of the given length, with all
52 * elements initially zero.
54 * @param length the length of the array
56 public AtomicLongArray(int length) {
57 array = new long[length];
61 * Creates a new AtomicLongArray with the same length as, and
62 * all elements copied from, the given array.
64 * @param array the array to copy elements from
65 * @throws NullPointerException if array is null
67 public AtomicLongArray(long[] array) {
68 // Visibility guaranteed by final field guarantees
69 this.array = array.clone();
73 * Returns the length of the array.
75 * @return the length of the array
77 public final int length() {
82 * Gets the current value at position {@code i}.
85 * @return the current value
87 public final long get(int i) {
92 * Sets the element at position {@code i} to the given value.
95 * @param newValue the new value
97 public final void set(int i, long newValue) {
102 * Eventually sets the element at position {@code i} to the given value.
105 * @param newValue the new value
108 public final void lazySet(int i, long newValue) {
114 * Atomically sets the element at position {@code i} to the given value
115 * and returns the old value.
118 * @param newValue the new value
119 * @return the previous value
121 public final long getAndSet(int i, long newValue) {
128 * Atomically sets the element at position {@code i} to the given
129 * updated value if the current value {@code ==} the expected value.
132 * @param expect the expected value
133 * @param update the new value
134 * @return true if successful. False return indicates that
135 * the actual value was not equal to the expected value.
137 public final boolean compareAndSet(int i, long expect, long update) {
138 if (array[i] == expect) {
147 * Atomically sets the element at position {@code i} to the given
148 * updated value if the current value {@code ==} the expected value.
150 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
151 * and does not provide ordering guarantees, so is only rarely an
152 * appropriate alternative to {@code compareAndSet}.
155 * @param expect the expected value
156 * @param update the new value
157 * @return true if successful.
159 public final boolean weakCompareAndSet(int i, long expect, long update) {
160 return compareAndSet(i, expect, update);
164 * Atomically increments by one the element at index {@code i}.
167 * @return the previous value
169 public final long getAndIncrement(int i) {
170 return getAndAdd(i, 1);
174 * Atomically decrements by one the element at index {@code i}.
177 * @return the previous value
179 public final long getAndDecrement(int i) {
180 return getAndAdd(i, -1);
184 * Atomically adds the given value to the element at index {@code i}.
187 * @param delta the value to add
188 * @return the previous value
190 public final long getAndAdd(int i, long delta) {
197 * Atomically increments by one the element at index {@code i}.
200 * @return the updated value
202 public final long incrementAndGet(int i) {
203 return addAndGet(i, 1);
207 * Atomically decrements by one the element at index {@code i}.
210 * @return the updated value
212 public final long decrementAndGet(int i) {
213 return addAndGet(i, -1);
217 * Atomically adds the given value to the element at index {@code i}.
220 * @param delta the value to add
221 * @return the updated value
223 public long addAndGet(int i, long delta) {
229 * Returns the String representation of the current values of array.
230 * @return the String representation of the current values of array
232 public String toString() {
233 int iMax = array.length - 1;
237 StringBuilder b = new StringBuilder();
239 for (int i = 0; ; i++) {
242 return b.append(']').toString();
243 b.append(',').append(' ');