AtomicInteger class in Java
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:
get()
: Gets the current value of theAtomicInteger
atomically.set(int newValue)
: Sets the value of theAtomicInteger
to a specified integer.getAndSet(int newValue)
: Atomically sets the value to a new integer and returns the old value.compareAndSet(int expect, int update)
: Atomically compares the current value to an expected value and updates it if the comparison succeeds.- 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:
AtomicLong
: For atomic operations onlong
values.AtomicBoolean
: For atomic operations onboolean
values.AtomicReference
: For atomic operations on reference types (objects).- Various atomic array classes like
AtomicIntegerArray
,AtomicLongArray
, andAtomicReferenceArray
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.