Java Concurrency - AtomicInteger Class

A java.util.concurrent.atomic.AtomicInteger class provides operations on underlying int value that can be read and written atomically, and also contains advanced atomic operations. AtomicInteger supports atomic operations on underlying int variable. It have get and set methods that work like reads and writes on volatile variables. That is, a set has a happens-before relationship with any subsequent get on the same variable. The atomic compareAndSet method also has these memory consistency features.
Following is the list of important methods available in the AtomicInteger class.
public int addAndGet(int delta)
Atomically adds the given value to the current value.
public boolean compareAndSet(int expect, int update)
Atomically sets the value to the given updated value if the current value is same as the expected value.
public int decrementAndGet()
Atomically decrements by one the current value.
public double doubleValue()
Returns the value of the specified number as a double.
public float floatValue()
Returns the value of the specified number as a float.
public int get()
Gets the current value.
public int getAndAdd(int delta)
Atomiclly adds the given value to the current value.
public int getAndDecrement()
Atomically decrements by one the current value.
public int getAndIncrement()

Atomically increments by one the current value.
public int getAndSet(int newValue)
Atomically sets to the given value and returns the old value.
public int incrementAndGet()
Atomically increments by one the current value.
public int intValue()
Returns the value of the specified number as an int.
public void  lazySet(int newValue)
Eventually sets to the given value.
public long  longValue()
Returns the value of the specified number as a long.
public void  set(int newValue)
Sets to the given value.
public String toString()
Returns the String representation of the current value.
public boolean weakCompareAndSet(int expect, int update)
Atomically sets the value to the given updated value if the current value is same as the expected value.
The following TestThread program shows a unsafe implementation of counter in thread based environment.
public class TestThread {

   static class Counter {
      private int c = 0;

      public void increment() {
         c++;
      }

      public int value() {
         return c;
      }
   }
   
   public static void main(final String[] arguments) throws InterruptedException {
      final Counter counter = new Counter();
      
      //1000 threads
      for(int i = 0; i < 1000 ; i++) {
         
         new Thread(new Runnable() {
            
            public void run() {
               counter.increment();
            }
         }).start(); 
      }  
      Thread.sleep(6000);
      System.out.println("Final number (should be 1000): " + counter.value());
   }  
}
This may produce the following result depending upon computer's speed and thread interleaving.
Final number (should be 1000): 1000

import java.util.concurrent.atomic.AtomicInteger;

public class TestThread {

   static class Counter {
      private AtomicInteger c = new AtomicInteger(0);

      public void increment() {
         c.getAndIncrement();
      }

      public int value() {
         return c.get();
      }
   }
   
   public static void main(final String[] arguments) throws InterruptedException {
      final Counter counter = new Counter();
      
      //1000 threads
      for(int i = 0; i < 1000 ; i++) {

         new Thread(new Runnable() {
            public void run() {
               counter.increment();
            }
         }).start(); 
      }  
      Thread.sleep(6000);
      System.out.println("Final number (should be 1000): " + counter.value());
   }
}
This will produce the following result.
Final number (should be 1000): 1000


    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page