Skip to content
Related Articles

Related Articles

Improve Article

AtomicInteger compareAndSet() method in Java with examples

  • Last Updated : 31 Jan, 2019

The java.util.concurrent.atomic.AtomicInteger.compareAndSet() is an inbuilt method in java that sets the value to the passed value in the parameter if the current value is equal to the expected value which is also passed in the parameter. The function returns a boolean value which gives us an idea if the update was done or not.

Syntax:

public final boolean compareAndSet(int expect, int val)

Parameters: The function accepts two mandatory parameters which are described below:

  • expect: which specifies the value that the atomic object should be.
  • val: which specifies the value to be updated if the atomic integer is equal to expect.

Return value: The function returns a boolean value, it returns true on success else false.



Program below demonstrates the function:

Program 1:




// Java Program to demonstrates
// the compareAndSet() function
  
import java.util.concurrent.atomic.AtomicInteger;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initially value as 0
        AtomicInteger val = new AtomicInteger(0);
  
        // Prints the updated value
        System.out.println("Previous value: "
                           + val);
  
        // Checks if previous value was 0
        // and then updates it
        boolean res = val.compareAndSet(0, 6);
  
        // Checks if the value was updated.
        if (res)
            System.out.println("The value was"
                               + " updated and it is "
                               + val);
        else
            System.out.println("The value was "
                               + "not updated");
    }
}
Output:
Previous value: 0
The value was updated and it is 6

Program 2:




// Java Program to demonstrates
// the compareAndSet() function
  
import java.util.concurrent.atomic.AtomicInteger;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initially value as 0
        AtomicInteger val
            = new AtomicInteger(0);
  
        // Prints the updated value
        System.out.println("Previous value: "
                           + val);
  
        // Checks if previous value was 0
        // and then updates it
        boolean res = val.compareAndSet(10, 6);
  
        // Checks if the value was updated.
        if (res)
            System.out.println("The value was"
                               + " updated and it is "
                               + val);
        else
            System.out.println("The value was "
                               + "not updated");
    }
}
Output:
Previous value: 0
The value was not updated

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html#compareAndSet–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :