Thursday, July 4, 2024
HomeLanguagesJavaAtomicReferenceArray getAndUpdate() method in Java with Examples

AtomicReferenceArray getAndUpdate() method in Java with Examples

The getAndUpdate() method of a AtomicReferenceArray class is used to atomically updates which updates the current value of the AtomicReferenceArray by applying the specified updateFunction operation on the current value. It takes an object of updateFunction interface as its parameter and applies the operation specified in the object to the current value. It returns the previous value.
Syntax: 
 

public final E 
  getAndUpdate(int i, 
    UnaryOperator<E> updateFunction)

Parameters: This method accepts: 
 

  • index i to update the index value and
  • updateFunction which is a side-effect-free function.

Return value: This method returns the previous value.
Below programs illustrate the getAndUpdate() method: 
Program 1: 
 

Java




// Java program to demonstrate
// AtomicReferenceArray.getAndUpdate() method
 
import java.util.concurrent.atomic.*;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
        // an array
        String a[]
            = { "GFG", "JS",
                "PYTHON", "JAVA" };
 
        // AtomicReferenceArray with array
        AtomicReferenceArray<String> array
            = new AtomicReferenceArray<>(a);
 
        // Print AtomicReferenceArray
        System.out.println(
            "The AtomicReferenceArray before update : "
            + array);
 
        // Index
        int index = 2;
 
        // Declaring the accumulatorFunction
        // applying function
        UnaryOperator add
            = (u) -> u.toString() + " and ML";
 
        // apply getAndUpdate()
        String value
            = array.getAndUpdate(index, add);
 
        // print AtomicReferenceArray
        System.out.println("previous value of index 2:"
                           + value);
        System.out.println("The AtomicReferenceArray "
                           + "after update: "
                           + array);
    }
}


Output: 

 

Program 2: 
 

Java




// Java program to demonstrate
// AtomicReferenceArray.getAndUpdate() method
 
import java.util.concurrent.atomic.*;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
        // an array
        Integer a[] = { 213, 1234, 4543, 345 };
 
        // AtomicReferenceArray with array
        AtomicReferenceArray<Integer> array
            = new AtomicReferenceArray(a);
 
        // Print AtomicReferenceArray
        System.out.println(
            "The AtomicReferenceArray"
            + " before update : "
            + array);
        // Index
        int index = 2;
 
        // Declaring the accumulatorFunction
        // applying function
        UnaryOperator add
            = (u) -> Integer.parseInt(u.toString()) * 200;
 
        // apply getAndUpdate()
        int value = array.getAndUpdate(index, add);
 
        // print AtomicReferenceArray
        System.out.println("previous value of index 2:"
                           + value);
        System.out.println("updated value of index 2:"
                           + array.get(2));
        System.out.println("The AtomicReferenceArray "
                           + "after update: "
                           + array);
    }
}


Output: 

 

References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#getAndUpdate(java.util.function.UnaryOperator)
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments