Java AtomicIntegerArray Example
In this example we shall talk about the AtomicIntegerArray class of Java. Java provides the java.util.concurrent.atomic package specification for lock-free and thread-safe programming on single variables. The AtomicIntegerArray class is an int
array in which elements may be updated atomically. Its difference with an simple AtomicInteger[]
is that it creates one object, whereas when using AtomicInteger[]
, one object per array element is created.
The AtomicIntegerArray class may be very useful when there is need to update simultaneously the elements of an array. So, in the example below, we have created four threads, AddFive
, Increment
, Insert
and Compare
. All threads run together and they all update the same AtomicIntegerArray at
. In short:
AddFive
uses theaddAndGet(int i, int delta)
API method to add the givendelta
to the element at indexi
.Increment
uses theincrementAndGet(int i)
API method, that increments by one the element at indexi
.Insert
uses theAtomicIntegerArray(int[] array)
constructor to create a newAtomicIntegerArray
object, with the same length as, and all elements copied from, the given array.Compare
uses thecompareAndSet(int i, int expect, int update)
API method to set the element at positioni
to the given updated value if the current value is equal to the expected value.
Note that all above methods act atomically, which means, that when one thread accesses one element of the array through one of the API methods, no other thread can interfere.
AtomicIntegerArrayExample.java:
package com.javacodegeeks.snippets.core; import java.util.concurrent.atomic.AtomicIntegerArray; public class AtomicIntegerArrayExample { private static AtomicIntegerArray at = new AtomicIntegerArray(10); public static void main(String[] args) throws InterruptedException { for (int i=0; i<at.length(); i++) { at.set(i, 1); } Thread t1 = new Thread(new AddFive()); Thread t2 = new Thread(new Increment()); Thread t3 = new Thread(new InsertArray()); Thread t4 = new Thread(new Compare()); t1.start(); t2.start(); t3.start(); t4.start(); t1.join(); t2.join(); t3.join(); t4.join(); System.out.println("All threads are finished. AtomicInteger array's values are : "); for (int i=0; i<at.length(); i++) { System.out.println(i + "-" + at.get(i)); } } static class AddFive implements Runnable { public void run() { for(int i=0; i<at.length(); i++) { int addFive = at.addAndGet(i, 5); System.out.println("Thread " + Thread.currentThread().getId() + " / adding five, at " + i + " position value is "+ addFive); } System.out.println("Thread " + Thread.currentThread().getId() + " / array now is : " + at); } } static class Increment implements Runnable { public void run() { for(int i=0; i<at.length(); i++) { int add = at.incrementAndGet(i); System.out.println("Thread " + Thread.currentThread().getId() + " / increasing, at " + i + " position value is "+ add); } System.out.println("Thread " + Thread.currentThread().getId() + " / array now is " + at); } } static class InsertArray implements Runnable { public void run() { int[] myArray = new int[3]; for(int i=0; i<3; i++) { myArray[i] = 5; } at = new AtomicIntegerArray(myArray); System.out.println("Thread " + Thread.currentThread().getId() + " Inseting new array, array now is " + at); } } static class Compare implements Runnable { public void run() { for(int i=0; i<at.length(); i++) { boolean isFive = at.compareAndSet(i, 5, 3); System.out.println("Thread " + Thread.currentThread().getId() + " / comparing value to 5, result is " + isFive + ", so at " + i + " position value is "+ at.get(i)); } System.out.println("Thread " + Thread.currentThread().getId() + " / array now is " + at); } } }
Run the application. The result is the one below:
Thread 9 / adding five, at 0 position value is 6 Thread 12 / comparing value to 5, result is true, so at 0 position value is 3 Thread 11 Inseting new array, array now is [5, 5, 5] Thread 10 / increasing, at 0 position value is 7 Thread 10 / increasing, at 1 position value is 11 Thread 12 / comparing value to 5, result is false, so at 1 position value is 10 Thread 9 / adding five, at 1 position value is 10 Thread 12 / comparing value to 5, result is false, so at 2 position value is 6 Thread 10 / increasing, at 2 position value is 6 Thread 12 / array now is [3, 11, 11] Thread 9 / adding five, at 2 position value is 11 Thread 10 / array now is [3, 11, 11] Thread 9 / array now is : [3, 11, 11] All threads are finished. AtomicInteger array's values are : 0-3 1-11 2-11
As you can see, all threads run simultaneously, but only one updates one element of the array at a time.
Download the Eclipse Project
This was an example of AtomicIntegerArray
class of Java.
You can download the full source code of this example here: AtomicIntegerArrayExample.zip