AtomicInteger in Java

AtomicInteger

AtomicInteger are classes in the java.util.concurrent.atomic package. It provides atomic (thread-safe) operations on underlying int fields.



This class provides you with an int value that can be read and written atomically, and contains advanced atomic operations. Atomic operations are performed in a single unit of task without interference from other operations. AtomicInteger supports atomic operations on underlying int variables such as compareAndSet, decrementAndGet, incrementAndGet, etc.

Let’s go through some of the key operations provided by the AtomicInteger class in Java.

  1. get(): This method returns the current value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.get());  // Output: 5
    
  2. set(int newValue): This method sets the value to the given updated value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    atomicInteger.set(10);
    System.out.println(atomicInteger.get());  // Output: 10
    
  3. getAndSet(int newValue): This method atomically sets the value to the given updated value and returns the old value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.getAndSet(10));  // Output: 5
    System.out.println(atomicInteger.get());  // Output: 10
    
  4. compareAndSet(int expect, int update): This method atomically sets the value to the given updated value if the current value equals the expected value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.compareAndSet(5, 10));  // Output: true
    System.out.println(atomicInteger.get());  // Output: 10
    
  5. getAndIncrement(): This method atomically increments the current value by one and returns the old value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.getAndIncrement());  // Output: 5
    System.out.println(atomicInteger.get());  // Output: 6
    
  6. getAndDecrement(): This method atomically decrements the current value by one and returns the old value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.getAndDecrement());  // Output: 5
    System.out.println(atomicInteger.get());  // Output: 4
    
  7. getAndAdd(int delta): This method atomically adds the given value to the current value and returns the old value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.getAndAdd(5));  // Output: 5
    System.out.println(atomicInteger.get());  // Output: 10
    
  8. incrementAndGet(): This method atomically increments the current value by one and returns the updated value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.incrementAndGet());  // Output: 6
    
  9. decrementAndGet(): This method atomically decrements the current value by one and returns the updated value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.decrementAndGet());  // Output: 4
    
  10. addAndGet(int delta): This method atomically adds the given value to the current value and returns the updated value.

    AtomicInteger atomicInteger = new AtomicInteger(5);
    System.out.println(atomicInteger.addAndGet(5));  // Output: 10
    

These are some of the key operations provided by the AtomicInteger class. They are all atomic, which means they are thread-safe and can be used in a multi-threaded environment without the need for explicit synchronization.




Post a Comment

Previous Post Next Post