AtomicIntegerArray
Jakob Jenkov |
The Java AtomicIntegerArray class (java.util.concurrent.atomic.AtomicIntegerArray)
represents an array of int . The int elements in the AtomicIntegerArray
can be updated atomically. The AtomicIntegerArray also supports
compare-and-swap functionality.
Creating an AtomicIntegerArray
You can create an AtomicIntegerArray using one of its two constructors.
The first constructor takes an int as parameter. This int specifies the length of the
AtomicIntegerArray to create, meaning how many elements it should have space for. Here is a Java
example of creating an AtomicIntegerArray using this constructor:
AtomicIntegerArray array = new AtomicIntegerArray(10);
This example creates a AtomicIntegerArray with a capacity of 10 ints (it has space for
10 int elements).
The second constructor takes an int[] array as parameter. The AtomicIntegerArray created
using this constructor will have the same capacity as the array parameter, and all elements from the array parameter
will be copied into the AtomicIntegerArray. Here is a Java example of creating an AtomicIntegerArray
using this constructor:
int[] ints = new int[10]; ints[5] = 123; AtomicIntegerArray array = new AtomicIntegerArray(ints);
This example first creates an int[] array and sets a value into the element with index 5.
Then it creates an AtomicIntegerArray with the int[] array as parameter.
get()
You can get the value of a given element using the get() method on the AtomicIntegerArray.
Here is an example showing how calling the get() method looks:
int value = array.get(5);
set()
You can set the value of a given element using the set() method on the AtomicIntegerArray.
Here is an example showing how calling the set() method looks:
array.set(5, 999);
compareAndSet()
The compareAndSet() method is used to compare the value of a given element with a specified value,
and if the two values are equal, set a new value for that element. This is an example of the atomic compare-and-swap
functionality support by the AtomicIntegerArray. Only one thread at a time can execute the
compareAndSet() method.
Here is how calling the compareAndSet()
method of the AtomicIntegerArray:
boolean swapped = array.compareAndSet(5, 999, 123);
This example compares the element with index 5 to the expected value 999, and if equal, sets the new value 123
for the element with index 5. The compareAndSet() method returns a boolean with the value
true if the element had a new value set, and false if not (if the element did not have the
expected value).
addAndGet()
The AtomicIntegerArray also contains a method that can be used to add a value to a given element,
and return the new value of the element. Calling addAndGet() is also an atomic operation (like all
operations on the AtomicIntegerArray). Here is an example of calling addAndGet() :
int newValue = array.addAndGet(5, 3);
After executing this code the newValue variable would contain the value of the element with index 5
with 3 added to it.
getAndAdd()
The AtomicIntegerArray class also contains a method named getAndAdd(). The getAndAdd()
method does the same as the addAndGet() method, except the getAndAdd() method returns the
value of the element before a value is added to it. Here is a getAndAdd() example:
int oldValue = array.getAndAdd(5, 3);
After executing this code the oldValue variable would contain the old value of the element with index 5,
before the value 3 was added to it.
incrementAndGet()
The incrementAndGet() method increments (adds 1) to the value of a given element and returns the new
value of that element. Here is a incrementAndGet() example:
int newValue = array.incrementAndGet(5);
After executing this code the newValue variable would contain the value of the element with index 5
with 1 added to it.
getAndIncrement()
The AtomicIntegerArray class also contains a method named getAndIncrement(). The
getAndIncrement()
method does the same as the incrementAndGet() method, except the getAndIncrement() method returns the
value of the element before it is incremented. Here is a getAndIncrement() example:
int oldValue = array.getAndIncrement(5);
After executing this code the oldValue variable would contain the old value of the element with index 5,
before it was incremented (1 was added to it).
decrementAndGet()
The decrementAndGet() method decrements (subtracts 1) to the value of a given element and returns the new
value of that element. Here is a decrementAndGet() example:
int newValue = array.decrementAndGet(5);
After executing this code the newValue variable would contain the value of the element with index 5
with 1 subtracted from it.
getAndDecrement()
The AtomicIntegerArray class also contains a method named getAndDecrement(). The
getAndDecrement()
method does the same as the decrementAndGet() method, except the getAndDecrement() method returns the
value of the element before it is decremented. Here is a getAndDecrement() example:
int oldValue = array.getAndDecrement(5);
After executing this code the oldValue variable would contain the old value of the element with index 5,
before it was decremented (1 was subtracted from it).
Additional Methods
The AtomicIntegerArray has several more methods that you can use for special purposes. You should
check out the JavaDoc for the AtomicIntegerArray class to learn more about those methods.
| Tweet | |
Jakob Jenkov | |











