AtomicIntegerArray

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 the addAndGet(int i, int delta) API method to add the given delta to the element at index i.
  • Increment uses the incrementAndGet(int i) API method, that increments by one the element at index i.
  • Insert uses the AtomicIntegerArray(int[] array) constructor to create a new AtomicIntegerArray object, with the same length as, and all elements copied from, the given array.
  • Compare uses the compareAndSet(int i, int expect, int update) API method to set the element at position i 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.

Download
You can download the full source code of this example here: AtomicIntegerArrayExample.zip

Theodora Fragkouli

Theodora has graduated from Computer Engineering and Informatics Department in the University of Patras. She also holds a Master degree in Economics from the National and Technical University of Athens. During her studies she has been involved with a large number of projects ranging from programming and software engineering to telecommunications, hardware design and analysis. She works as a junior Software Engineer in the telecommunications sector where she is mainly involved with projects based on Java and Big Data technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button