AtomicIntegerArray compareAndSet() method in Java with Examples

The Java.util.concurrent.atomic.AtomicIntegerArray.compareAndSet() is an inbuilt method in java that atomically sets the element at a position to the given updated value if the current value is equal to the expected value. This method takes the index value, the expected value and the update value as the parameters and returns a boolean value stating if the value has been updated.

Syntax:

public final boolean compareAndSet(int i, int expect, int update)



Parameters: The function accepts three parameters:

  • i: The index where operation is to be made.
  • expect: The expected value to check if it is equal to current value.
  • update: The value to be updated.

Return value: The function returns a boolean value stating if the value has been updated. It returns true if successful, else it returns false indicating that the current value was not equal to the expected value.

Below programs illustrate the above method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates
// the compareAndSet() function
  
import java.util.concurrent.atomic.AtomicIntegerArray;
  
public class GFG {
    public static void main(String args[])
    {
        // Initializing an array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Initializing an AtomicIntegerArray with array a
        AtomicIntegerArray arr = new AtomicIntegerArray(a);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array : " + arr);
  
        // Index where operation is performed
        int idx = 3;
  
        // Value to expect at idx
        int expect = 4;
  
        // Value to update if current value
        // is equal to expected value
        int update = 40;
  
        // Updating the value at idx
        // applying compareAndSet
        arr.compareAndSet(idx, expect, update);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array after update : "
                           + arr);
    }
}

chevron_right


Output:

The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 40, 5]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates
// the compareAndSet() function
  
import java.util.concurrent.atomic.AtomicIntegerArray;
  
public class GFG {
    public static void main(String args[])
    {
        // Initializing an array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Initializing an AtomicIntegerArray with array a
        AtomicIntegerArray arr = new AtomicIntegerArray(a);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array : " + arr);
  
        // Index where operation is performed
        int idx = 3;
  
        // Value to expect at idx
        int expect = 40;
  
        // Value to update if current value
        // is equal to expected value
        int update = 4;
  
        // Updating the value at
        // idx applying compareAndSet
        arr.compareAndSet(idx, expect, update);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array after update : "
                           + arr);
    }
}

chevron_right


Output:

The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 4, 5]

Reference: The Java.util.concurrent.atomic.AtomicIntegerArray.compareAndSet() is an inbuilt method in java that atomically sets the element at a position to the given updated value if the current value is equal to the expected value. This method takes the index value, the expected value and the update value as the parameters and returns a boolean value stating if the value has been updated.

Syntax:

public final boolean compareAndSet(int i, Integer expect, Integer update)

Parameters: The function accepts three parameters:


  • i: The index where operation is to be made.
  • expect: The expected value to check if it is equal to current value.
  • update: The value to be updated.
  • Return value: The function returns a boolean value stating if the value has been updated. It returns true if successful, else it returns false indicating that the current value was not equal to the expected value.

    Below programs illustrate the above method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program that demonstrates
    // the compareAndSet() function
      
    import java.util.concurrent.atomic.AtomicIntegerArray;
      
    public class GFG {
        public static void main(String args[])
        {
            // Initializing an array
            Integer a[] = { 1, 2, 3, 4, 5 };
      
            // Initializing an AtomicIntegerArray with array a
            AtomicIntegerArray arr = new AtomicIntegerArray(a);
      
            // Displaying the AtomicIntegerArray
            System.out.println("The array : " + arr);
      
            // Index where operation is performed
            int idx = 3;
      
            // Value to expect at idx
            Integer expect = 4;
      
            // Value to update if current value
            // is equal to expected value
            Integer update = 40;
      
            // Updating the value at idx
            // applying compareAndSet
            arr.compareAndSet(idx, expect, update);
      
            // Displaying the AtomicIntegerArray
            System.out.println("The array after update : "
                               + arr);
        }
    }

    chevron_right

    
    

    Output:

    The array : [1, 2, 3, 4, 5]
    The array after update : [1, 2, 3, 40, 5]
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program that demonstrates
    // the compareAndSet() function
      
    import java.util.concurrent.atomic.AtomicIntegerArray;
      
    public class GFG {
        public static void main(String args[])
        {
            // Initializing an array
            Integer a[] = { 1, 2, 3, 4, 5 };
      
            // Initializing an AtomicIntegerArray with array a
            AtomicIntegerArray arr = new AtomicIntegerArray(a);
      
            // Displaying the AtomicIntegerArray
            System.out.println("The array : " + arr);
      
            // Index where operation is performed
            int idx = 3;
      
            // Value to expect at idx
            Integer expect = 40;
      
            // Value to update if current value
            // is equal to expected value
            Integer update = 4;
      
            // Updating the value at
            // idx applying compareAndSet
            arr.compareAndSet(idx, expect, update);
      
            // Displaying the AtomicIntegerArray
            System.out.println("The array after update : "
                               + arr);
        }
    }

    chevron_right

    
    

    Output:

    The array : [1, 2, 3, 4, 5]
    The array after update : [1, 2, 3, 4, 5]
    

    Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#compareAndSet(int, %20int, %20int)



    My Personal Notes arrow_drop_up

    The function of education is to teach one to think intensively and to think critically

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.