AtomicReferenceArray compareAndExchange() method in Java with Examples

The compareAndExchange() method of a AtomicReferenceArray class is used to atomically sets the value of index i of AtomicReferenceArray to newValue, if the current value at index i of AtomicReferenceArray object which is referred to as the witness value is equal to the expectedValue. This method will return the witness value, which will be the same as the expected value. This method handles the operation with memory semantics of reading as if the variable was declared volatile.

Syntax:

public final boolean
       compareAndExchange(
           int i, E expectedValue, E newValue)

Parameters: This method accepts:

  • i which is an index of AtomicReferenceArray to perform the operation,
  • expectedValue which is the expected value and
  • newValue which is the new value to set.

Return value: This method returns the witness value, which will be the same as the expected value if successful.

Below programs illustrate the compareAndExchange() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// compareAndExchange() method
  
import java.util.concurrent.atomic.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create an atomic reference object.
        AtomicReferenceArray<Integer> ref
            = new AtomicReferenceArray<Integer>(3);
  
        // set some value
        ref.set(0, 1234);
        ref.set(1, 4322);
  
        // apply compareAndExchange()
        int oldV1
            = ref.compareAndExchange(0, 1234, 8913);
        int oldV2
            = ref.compareAndExchange(1, 4322, 6543);
  
        // print
        System.out.println("Old value at index 0: "
                           + oldV1);
        System.out.println("Old value at index 1: "
                           + oldV2);
    }
}

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// compareAndExchange() method
  
import java.util.concurrent.atomic.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create an atomic reference object.
        AtomicReferenceArray<String> ref
            = new AtomicReferenceArray<String>(3);
  
        // set some value
        ref.set(0, "GFG");
        ref.set(1, "JS");
  
        // apply compareAndExchange()
        String oldV1
            = ref.compareAndExchange(
                0, "GFG",
                "GEEKS FOR GEEKS");
        String oldV2
            = ref.compareAndExchange(
                1, "JS",
                "JAVA SCRIPT");
  
        // print
        System.out.println("Old value at index 0: "
                           + oldV1);
        System.out.println("New value at index 0: "
                           + ref.get(0));
        System.out.println("Old value at index 1: "
                           + oldV2);
        System.out.println("New value at index 1: "
                           + ref.get(1));
    }
}

chevron_right


Output:

References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#compareAndExchange(int, E, E)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.