Java - How to use AtomicIntegerArray

AtomicIntegerArray is a wrapping class that has an int[] data type.

Concurrency can be guaranteed at a lower cost than synchronized in a multithreaded environment.

Let`s see how to use AtomicIntegerArray.

create object

AtomicIntegerArray can be created like this:

  • Passing an int[] to the argument of the constructor => creates an int[] array with the same size and value inside
  • Passing the length of the array to the argument of the constructor => An array is created inside by the length, and the initial value is set to 0
public void atomicIntegerArray1() {
    int arr[] = { 1, 2, 3, 4, 5 };
    AtomicIntegerArray atomic = new AtomicIntegerArray(arr);
    System.out.println("arr : " + atomic);

    AtomicIntegerArray atomic2 = new AtomicIntegerArray(5);
    System.out.println("arr : " + atomic2);
}

result

arr : [1, 2, 3, 4, 5]
arr : [0, 0, 0, 0, 0]

get(), set(), getAndSet()

You must use the set(index, newValue) method to change the value of the AtomicIntegerArray, and the get(index) method to read the value. getAndSet(index, newValue) returns the current value and updates it with the new value.

public void atomicIntegerArray2() {
    int arr[] = { 1, 2, 3, 4, 5 };
    AtomicIntegerArray atomic = new AtomicIntegerArray(arr);
    System.out.println("arr : " + atomic);

    int index = 1;
    System.out.println("get(i = 1) : " + atomic.get(index));

    index = 3;
    int newValue = 10;
    atomic.set(index, newValue);
    System.out.println("get(i = 3) : " + atomic.get(index));

    index = 4;
    System.out.println("getAndSet(i = 4) : " + atomic.getAndSet(index, newValue));
    System.out.println("get(i = 4) : " + atomic.get(index));

    System.out.println("arr : " + atomic);
}

result

arr : [1, 2, 3, 4, 5]
get(i = 1) : 2
get(i = 3) : 10
getAndSet(i = 4) : 5
get(i = 4) : 10
arr : [1, 2, 3, 10, 10]

getAndUpdate()

getAndUpdate(index, IntUnaryOperator) is a bit different from getAndSet(). You can pass a lambda expression as the second argument, so the function can change its value.

public void atomicIntegerArray3() {
    int arr[] = { 1, 2, 3, 4, 5 };
    AtomicIntegerArray atomic = new AtomicIntegerArray(arr);
    System.out.println("arr : " + atomic);

    int index = 3;
    IntUnaryOperator square = (i) -> i * i;

    System.out.println("value(before update, i = 3) : " + atomic.getAndUpdate(index, square));
    System.out.println("value(after update, i = 3) : " + atomic.get(index));
    System.out.println("arr : " + atomic);
}

result

arr : [1, 2, 3, 4, 5]
value(before update, i = 3) : 4
value(after update, i = 3) : 16
arr : [1, 2, 3, 16, 5]

getAndIncrement(), getAndAdd()

  • getAndIncrement(index) : Returns the current value, + to the variable
  • getAndDecrement(index) : Returns the current value, and -
  • getAndAdd(index, newValue) : Returns the current value, adds newValue
  • addAndGet(index, newValue) : Adds newValue and returns the result
public void atomicIntegerArray4() {
    int arr[] = { 1, 2, 3, 4, 5 };
    AtomicIntegerArray atomic = new AtomicIntegerArray(arr);
    System.out.println("arr : " + atomic);

    int index = 3;
    System.out.println("getAndIncrement(i = 3) : " + atomic.getAndIncrement(index));
    System.out.println("getAndIncrement(i = 3) : " + atomic.getAndIncrement(index));
    System.out.println("get(i = 3) : " + atomic.get(index));

    System.out.println("getAndDecrement(i = 3) : " + atomic.getAndDecrement(index));
    System.out.println("getAndDecrement(i = 3) : " + atomic.getAndDecrement(index));
    System.out.println("get(i = 3) : " + atomic.get(index));

    System.out.println("getAndAdd(i = 3) : " + atomic.getAndAdd(index, 10));
    System.out.println("get(i = 3) : " + atomic.get(index));

    System.out.println("addAndGet(i = 3) : " + atomic.addAndGet(index, 10));
    System.out.println("get(i = 3) : " + atomic.get(index));

    System.out.println("arr : " + atomic);
}

result

arr : [1, 2, 3, 4, 5]
getAndIncrease(i = 3) : 4
getAndIncrease(i = 3) : 5
get(i = 3) : 6
getAndDecrement(i = 3) : 6
getAndDecrement(i = 3) : 5
get(i = 3) : 4
getAndAdd(i = 3) : 4
get(i = 3) : 14
addAndGet(i = 3) : 24
get(i = 3) : 24
arr : [1, 2, 3, 24, 5]

compareAndSet()

compareAndSet(index, expect, update) returns true if the current value is the same as the expected value (expect), changing it to the update value. Otherwise, there is no data change and returns false.

/**
 * Atomically sets the element at position {@code i} to the given
 * updated value if the current value {@code ==} the expected value.
 *
 * @param i the index
 * @param expect the expected value
 * @param update the new value
 * @return {@code true} if successful. False return indicates that
 * the actual value was not equal to the expected value.
 */
public final boolean compareAndSet(int i, int expect, int update)

Here is an example using compareAndSet(). The data is updated only when it matches the current value and true is returned.

public void atomicIntegerArray5() {
    int arr[] = { 1, 2, 3, 4, 5 };
    AtomicIntegerArray atomic = new AtomicIntegerArray(arr);
    System.out.println("arr : " + atomic);

    int index = 3;
    int expected = 3;
    int update = 40;

    System.out.println("success ? " + atomic.compareAndSet(index, expected, update));

    expected = 4;
    System.out.println("success ? " + atomic.compareAndSet(index, expected, update));

    System.out.println("arr : " + atomic);
}

result

arr : [1, 2, 3, 4, 5]
success ? false
success ? true
arr : [1, 2, 3, 40, 5]

Reference

codechachaCopyright ©2019 codechacha