The while loop ensures that the value is always incremented by 1 from the previous value. Many of the methods in the atomic classes use a similar strategy to implement atomic operations.

It goes without saying that the AtomicInteger class should not be used as a replacement for the Integer class. We leave the inquisitive reader to explore the Atomic API further at leisure.

Example 23.10 Atomic Counter

Click here to view code image

package safe;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicCounter implements ICounter {
  private AtomicInteger counter = new AtomicInteger(0);                   // (1)
  @Override
  public int getValue() {                                                 // (2)
    return counter.get();
  }
  @Override
  public void increment() {                                               // (3)
    counter.incrementAndGet();                                            // (4)
//  while (true) {                                                        // (5)
//    int expectedValue = counter.get();                                  // (6)
//    int newValue = expectedValue + 1;                                   // (7)
//    if (counter.compareAndSet(expectedValue, newValue)) {// (8) Compare and Set.
//      return;                                            // (9)
//    }
//  }
  }
}

Output from the program in Example 23.7, p. 1451:

Click here to view code image

Atomic Counter:                    10000

The AtomicInteger class provides the following constructors:

Click here to view code image

AtomicInteger()
AtomicInteger(int initialValue)

Create a new AtomicInteger with initial value 0 or with the given initial value, respectively.

Selected atomic methods from the AtomicInteger class are presented below.

int get()

Returns the current value.

void set(int newValue)

Sets the value to newValue.

int getAndSet(int newValue)

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

Click here to view code image

boolean compareAndSet(int expectedValue, int newValue)

Atomically sets the value to newValue if the current value == expectedValue, returning true if the operation was successful; otherwise, it returns false.

int addAndGet(int delta)
int getAndAdd(int delta)

Atomically adds the given value to the current value, returning the updated value or the previous value, respectively.

int decrementAndGet()
int getAndDecrement()

Atomically decrements the current value, returning the updated value or the previous value, respectively.

int incrementAndGet()
int getAndIncrement()

Atomically increments the current value, returning the updated value or the previous value, respectively.

Click here to view code image

int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction)
int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction)

Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value or the previous value, respectively.

Click here to view code image

int updateAndGet(IntUnaryOperator updateFunction)
int getAndUpdate(IntUnaryOperator updateFunction)

Atomically updates the current value with the results of applying the given function, returning the updated value or the previous value, respectively.

int    intValue()
double doubleValue()
float  floatValue()
long   longValue()

Returns the current value of this AtomicInteger as an int, double, float, and long after a widening primitive conversion, if necessary, respectively.