Java AtomicInteger
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 ofAtomicInteger
can be created in two ways.
1. With initial default value as 0.
AtomicInteger ai = new AtomicInteger();
AtomicInteger(int initialValue)
AtomicInteger ai = new AtomicInteger(15);
set() and get()
a.final void set(int newValue)
b.
final int get()
Ex.
AtomicInteger ai = new AtomicInteger(); ai.set(111); int intVal = ai.get(); System.out.println(intVal); // 111
addAndGet
final int addAndGet(int delta)
Ex.
int intVal = ai.addAndGet(10); System.out.println(intVal); // 10 intVal = ai.addAndGet(-5); System.out.println(intVal); // 5
incrementAndGet
final int incrementAndGet()
Ex.
AtomicInteger ai = new AtomicInteger(10); int intVal = ai.incrementAndGet(); System.out.println(intVal); // 11
decrementAndGet
final int decrementAndGet()
Ex.
AtomicInteger ai = new AtomicInteger(10); int intVal = ai.decrementAndGet(); System.out.println(intVal); // 9
compareAndSet
final boolean compareAndSet(int expectedValue, int newValue)
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)
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
AtomicInteger ai = new AtomicInteger(10); int intVal = ai.accumulateAndGet(20, (x, y) -> x + y); System.out.println(intVal); // 30
Example with Multi-Threads
TheAtomicInteger
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(); } }