Java AtomicInteger .compareAndSet (int expect, int update)

Syntax

AtomicInteger.compareAndSet(int expect, int update) has the following syntax.

public final boolean compareAndSet(int expect,   int update)

Example

In the following code shows how to use AtomicInteger.compareAndSet(int expect, int update) method.


import java.util.concurrent.atomic.AtomicInteger;
//from w  w  w. j  av  a2 s  .  com
public class Main {
  public static void main(String[] argv) throws Exception {
    AtomicInteger atomicInteger = new AtomicInteger();
    atomicInteger.compareAndSet(10,10);
    System.out.println(atomicInteger.decrementAndGet());
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.util.concurrent.atomic »




AtomicBoolean
AtomicInteger
AtomicIntegerArray
AtomicLong
AtomicLongArray