Java AtomicLong.compareAndSet(long expect, long update)

Syntax

AtomicLong.compareAndSet(long expect, long update) has the following syntax.

public final boolean compareAndSet(long expect,   long update)

Example

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


/*from  www . ja  v a2 s  . com*/
import java.util.concurrent.atomic.AtomicLong;

public class Main {
  public static void main(String[] argv) {
    AtomicLong nextId = new AtomicLong();


    nextId.compareAndSet(122L,123L);
    System.out.println(nextId.getAndIncrement());
    
    
    
  }
}

The code above generates the following result.





















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




AtomicBoolean
AtomicInteger
AtomicIntegerArray
AtomicLong
AtomicLongArray