Home  >  Core Java

Java AtomicInteger Example

By Arvind Rai, May 12, 2022
On this page we will learn using Java AtomicInteger class.
1. Java AtomicInteger is an int value that may be updated atomically.
2. The AtomicInteger is used in the application where we need atomically incremented counters. We should use it in the application when we have multiple threads accessing int variable.
3. The AtomicInteger variable is lock-free and thread-safe. Instances of AtomicInteger maintain values that are accessed and updated using its methods.
4. The AtomicInteger should not be used as the replacement of Integer.
5. The AtomicInteger is introduced since Java 5.
6. The AtomicInteger has methods such as set, get, addAndGet, incrementAndGet, decrementAndGet, compareAndSet, accumulateAndGet etc.

Creating Instance

The instances of AtomicInteger can be created in two ways.
1. With initial default value as 0.
AtomicInteger ai = new AtomicInteger(); 
2. With given initial value.
AtomicInteger(int initialValue) 
We can create instance as following.
AtomicInteger ai = new AtomicInteger(15); 
Here initial value is 15.

set() and get()

a.
final void set(int newValue) 
Sets the value to newValue.
b.
final int get() 
Returns the current value.
Ex.
AtomicInteger ai = new AtomicInteger();
ai.set(111);
int intVal = ai.get();
System.out.println(intVal); // 111 

addAndGet

final int addAndGet(int delta) 
Atomically adds the given value to the current value
Ex.
int intVal = ai.addAndGet(10);
System.out.println(intVal); // 10
intVal = ai.addAndGet(-5);
System.out.println(intVal); // 5 

incrementAndGet

final int incrementAndGet() 
Atomically increments the current value by 1.
Ex.
AtomicInteger ai = new AtomicInteger(10);
int intVal = ai.incrementAndGet();
System.out.println(intVal); // 11 

decrementAndGet

final int decrementAndGet() 
Atomically decrements the current value by 1.
Ex.
AtomicInteger ai = new AtomicInteger(10);
int intVal = ai.decrementAndGet();
System.out.println(intVal); // 9 

compareAndSet

final boolean compareAndSet(int expectedValue, int newValue) 
Atomically sets the value to newValue if the current value == expectedValue.
Ex.
AtomicInteger ai = new AtomicInteger(10);
System.out.println(ai.get()); // 10

boolean isSuccess = ai.compareAndSet(15, 25); //(current value != expectedValue)
System.out.println(isSuccess); //false
System.out.println(ai.get()); // 10 (Value not changed)

isSuccess = ai.compareAndSet(10, 25); // current value == expectedValue
System.out.println(isSuccess); //true
System.out.println(ai.get()); // 25 (Value changed) 

accumulateAndGet

final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) 
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.
Ex-1
AtomicInteger ai = new AtomicInteger(10);
int intVal = ai.accumulateAndGet(5, (x, y) -> {
	       System.out.println("Current Val:" + x); // Current Val:10
	       System.out.println("Given Val:" + y); // Given Val:5
	       return x * y;
	});
System.out.println(intVal); // 50 
Ex-2
AtomicInteger ai = new AtomicInteger(10);
int intVal = ai.accumulateAndGet(20, (x, y) -> x + y);
System.out.println(intVal); // 30 

Example with Multi-Threads

The AtomicInteger is made for concurrency environment. It is thread-safe. Find one example with multi-threads.
AtomicIntegerDemo.java
package com.concretepage;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerDemo {
  AtomicInteger ai = new AtomicInteger(10);
  class AddThread implements Runnable {
	@Override
	public void run() {
	  System.out.println(ai.incrementAndGet());
	}
  }
  class SubThread implements Runnable {
	@Override
	public void run() {
	  System.out.println(ai.decrementAndGet());
	}
  }
  public static void main(String... args) {
	AtomicIntegerDemo ob = new AtomicIntegerDemo();
	new Thread(ob.new AddThread()).start();
	new Thread(ob.new SubThread()).start();
  }
} 

Reference

Class AtomicInteger
POSTED BY
ARVIND RAI
ARVIND RAI











©2022 concretepage.com | Privacy Policy | Contact Us