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;
41
* An {@code int} array in which elements may be updated atomically.
42
* See the {@link java.util.concurrent.atomic} package
43
* specification for description of the properties of atomic
48
public class AtomicIntegerArray implements java.io.Serializable {
49
private static final long serialVersionUID = 2862133569453604235L;
51
private final int[] array;
54
* Creates a new AtomicIntegerArray of the given length, with all
55
* elements initially zero.
57
* @param length the length of the array
59
public AtomicIntegerArray(int length) {
60
array = new int[length];
64
* Creates a new AtomicIntegerArray with the same length as, and
65
* all elements copied from, the given array.
67
* @param array the array to copy elements from
68
* @throws NullPointerException if array is null
70
public AtomicIntegerArray(int[] array) {
71
// Visibility guaranteed by final field guarantees
72
this.array = array.clone();
76
* Returns the length of the array.
78
* @return the length of the array
80
public final int length() {
85
* Gets the current value at position {@code i}.
88
* @return the current value
90
public final native int get(int i);
93
* Sets the element at position {@code i} to the given value.
96
* @param newValue the new value
98
public final native void set(int i, int newValue);
101
* Eventually sets the element at position {@code i} to the given value.
104
* @param newValue the new value
107
public final void lazySet(int i, int newValue) {
112
* Atomically sets the element at position {@code i} to the given
113
* value and returns the old value.
116
* @param newValue the new value
117
* @return the previous value
119
public final native int getAndSet(int i, int newValue);
122
* Atomically sets the element at position {@code i} to the given
123
* updated value if the current value {@code ==} the expected value.
126
* @param expect the expected value
127
* @param update the new value
128
* @return true if successful. False return indicates that
129
* the actual value was not equal to the expected value.
131
public final native boolean compareAndSet(int i, int expect, int update);
134
* Atomically sets the element at position {@code i} to the given
135
* updated value if the current value {@code ==} the expected value.
137
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
138
* and does not provide ordering guarantees, so is only rarely an
139
* appropriate alternative to {@code compareAndSet}.
142
* @param expect the expected value
143
* @param update the new value
144
* @return true if successful.
146
public final boolean weakCompareAndSet(int i, int expect, int update) {
147
return compareAndSet(i, expect, update);
151
* Atomically increments by one the element at index {@code i}.
154
* @return the previous value
156
public final int getAndIncrement(int i) {
157
return incrementAndGet(i) - 1;
161
* Atomically decrements by one the element at index {@code i}.
164
* @return the previous value
166
public final int getAndDecrement(int i) {
167
return decrementAndGet(i) + 1;
171
* Atomically adds the given value to the element at index {@code i}.
174
* @param delta the value to add
175
* @return the previous value
177
public final int getAndAdd(int i, int delta) {
178
return addAndGet(i, delta) - delta;
182
* Atomically increments by one the element at index {@code i}.
185
* @return the updated value
187
public final native int incrementAndGet(int i);
190
* Atomically decrements by one the element at index {@code i}.
193
* @return the updated value
195
public final native int decrementAndGet(int i);
198
* Atomically adds the given value to the element at index {@code i}.
201
* @param delta the value to add
202
* @return the updated value
204
public final native int addAndGet(int i, int delta);
207
* Returns the String representation of the current values of array.
208
* @return the String representation of the current values of array.
210
public String toString() {
211
int iMax = array.length - 1;
215
StringBuilder b = new StringBuilder();
217
for (int i = 0; ; i++) {
220
return b.append(']').toString();
221
b.append(',').append(' ');