AtomicInteger class in Java

Saurav Kumar
2 min readSep 18, 2023

In Java, the AtomicInteger class is part of the java.util.concurrent.atomic package, which provides a set of atomic variables that support atomic operations. An atomic operation is an operation that is executed as a single, uninterruptible unit, ensuring thread safety in a multithreaded environment. The AtomicInteger class specifically deals with atomic operations on integer values.

The AtomicInteger class provides methods to perform operations like:

  1. get(): Gets the current value of the AtomicInteger atomically.
  2. set(int newValue): Sets the value of the AtomicInteger to a specified integer.
  3. getAndSet(int newValue): Atomically sets the value to a new integer and returns the old value.
  4. compareAndSet(int expect, int update): Atomically compares the current value to an expected value and updates it if the comparison succeeds.
  5. Various atomic arithmetic operations like incrementAndGet(), decrementAndGet(), addAndGet(int delta), and more.

Here’s an example of using AtomicInteger to perform atomic increments:

package com.tipsontech.demo;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerExample {
public static void main(String[] args) {
// Create an AtomicInteger with an initial value of 5
AtomicInteger atomicInt = new AtomicInteger(5);

// Demonstrate the get() method
int currentValue = atomicInt.get();
System.out.println("Current Value: " + currentValue);

// Demonstrate the set(int newValue) method
atomicInt.set(10);
System.out.println("Set to 10. Current Value: " + atomicInt.get());

// Demonstrate the getAndSet(int newValue) method
int oldValue = atomicInt.getAndSet(20);
System.out.println("Old Value (getAndSet): " + oldValue);
System.out.println("New Value (getAndSet): " + atomicInt.get());

// Demonstrate the compareAndSet(int expect, int update) method
boolean updated = atomicInt.compareAndSet(20, 30);
System.out.println("Updated: " + updated);
System.out.println("Current Value (compareAndSet): " + atomicInt.get());

// Demonstrate atomic arithmetic operations
int incrementedValue = atomicInt.incrementAndGet();
System.out.println("Incremented Value: " + incrementedValue);

int decrementedValue = atomicInt.decrementAndGet();
System.out.println("Decremented Value: " + decrementedValue);

int addedValue = atomicInt.addAndGet(5);
System.out.println("Added Value: " + addedValue);
}
}

Here’s an output of the above code:

Current Value: 5
Set to 10. Current Value: 10
Old Value (getAndSet): 10
New Value (getAndSet): 20
Updated: true
Current Value (compareAndSet): 30
Incremented Value: 31
Decremented Value: 30
Added Value: 35

In addition to AtomicInteger, Java provides similar atomic classes for other data types such as:

  1. AtomicLong: For atomic operations on long values.
  2. AtomicBoolean: For atomic operations on boolean values.
  3. AtomicReference: For atomic operations on reference types (objects).
  4. Various atomic array classes like AtomicIntegerArray, AtomicLongArray, and AtomicReferenceArray for atomic operations on arrays of respective types.

These atomic classes are useful for building thread-safe, concurrent data structures and for ensuring that operations on shared data are performed atomically without explicit synchronization using locks.

--

--

Saurav Kumar

Experienced Software Engineer adept in Java, Spring Boot, Microservices, Kafka & Azure.