English 中文(简体)
Concurrency - AtomicInteger
  • 时间:2024-09-17

Java Concurrency - AtomicInteger Class


Previous Page Next Page  

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 pke 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.

AtomicInteger Methods

Following is the pst of important methods available in the AtomicInteger class.

Sr.No. Method & Description
1

pubpc int addAndGet(int delta)

Atomically adds the given value to the current value.

2

pubpc 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.

3

pubpc int decrementAndGet()

Atomically decrements by one the current value.

4

pubpc double doubleValue()

Returns the value of the specified number as a double.

5

pubpc float floatValue()

Returns the value of the specified number as a float.

6

pubpc int get()

Gets the current value.

7

pubpc int getAndAdd(int delta)

Atomiclly adds the given value to the current value.

8

pubpc int getAndDecrement()

Atomically decrements by one the current value.

9

pubpc int getAndIncrement()

Atomically increments by one the current value.

10

pubpc int getAndSet(int newValue)

Atomically sets to the given value and returns the old value.

11

pubpc int incrementAndGet()

Atomically increments by one the current value.

12

pubpc int intValue()

Returns the value of the specified number as an int.

13

pubpc void lazySet(int newValue)

Eventually sets to the given value.

14

pubpc long longValue()

Returns the value of the specified number as a long.

15

pubpc void set(int newValue)

Sets to the given value.

16

pubpc String toString()

Returns the String representation of the current value.

17

pubpc 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.

Example

The following TestThread program shows a unsafe implementation of counter in thread based environment.

pubpc class TestThread {

   static class Counter {
      private int c = 0;

      pubpc void increment() {
         c++;
      }

      pubpc int value() {
         return c;
      }
   }
   
   pubpc 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() {
            
            pubpc 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.

Output

Final number (should be 1000): 1000

Example

The following TestThread program shows a safe implementation of counter using AtomicInteger in thread based environment.
import java.util.concurrent.atomic.AtomicInteger;

pubpc class TestThread {

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

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

      pubpc int value() {
         return c.get();
      }
   }
   
   pubpc 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() {
            pubpc void run() {
               counter.increment();
            }
         }).start(); 
      }  
      Thread.sleep(6000);
      System.out.println("Final number (should be 1000): " + counter.value());
   }
}

This will produce the following result.

Output

Final number (should be 1000): 1000
Advertisements