AtomicReference updateAndGet() method in Java with Examples Last Updated : 03 Jan, 2020 Comments Improve Suggest changes Like Article Like Report The updateAndGet() method of a AtomicReference class is used to atomically updates which updates the current value of the AtomicReference 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 updated value. Syntax: public final V updateAndGet(UnaryOperator<V> updateFunction) Parameters: This method accepts updateFunction which is a side-effect-free function. Return value: This method returns the updated value. Below programs illustrate the updateAndGet() method: Program 1: Java // Java program to demonstrate // AtomicReference.updateAndGet() method import java.util.concurrent.atomic.*; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<Integer> ref = new AtomicReference<>(987654); // Declaring the updateFunction // applying function UnaryOperator function = (v) -> Integer.parseInt(v.toString()) * 2; // apply updateAndGet() int value = ref.updateAndGet(function); // print AtomicReference System.out.println( "The AtomicReference updated value: " + value); } } Output: Program 2: Java // Java program to demonstrate // AtomicReference.updateAndGet() method import java.util.concurrent.atomic.*; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<String> ref = new AtomicReference<>("welcome"); // Declaring the updateFunction // applying function to add value as string UnaryOperator twoDigits = (v) -> v + " to gfg"; // apply updateAndGet() String value = ref.updateAndGet(twoDigits); // print AtomicReference System.out.println( "The AtomicReference current value: " + value); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#updateAndGet(java.util.function.UnaryOperator) Comment More infoAdvertise with us Next Article AtomicReference updateAndGet() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Practice Tags : Java Similar Reads AtomicReferenceArray updateAndGet() method in Java with Examples The updateAndGet() 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 2 min read AtomicInteger updateAndGet() method in Java with Examples The Java.AtomicInteger.updateAndGet() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value. It takes an object of IntUnaryOperator interface as its parameter and applies the operation specified in the object to the curren 1 min read AtomicLong updateAndGet() method in Java with Examples The Java.AtomicLong.updateAndGet() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value. It takes an object of LongUnaryOperator interface as its parameter and applies the operation specified in the object to the current 1 min read AtomicReference set() method in Java with Examples The set() method of a AtomicReference class is used to set the value of this AtomicReference object with memory semantics of reading as if the variable was declared volatile type of variable. Syntax: public final void set(V newValue) Parameters: This method accepts newValue which is the new value to 1 min read AtomicReference weakCompareAndSet() method in Java with Examples The weakCompareAndSet() method of a AtomicReference class is used to atomically sets the value to newValue for AtomicReference if the current value is equal to expectedValue passed as parameter. This method update the value with memory semantics of reading as if the variable was declared volatile ty 2 min read AtomicReference setRelease() method in Java with Examples The setRelease() method of a AtomicReference class is used to set the value of this AtomicReference object with memory effects as specified by VarHandle.setRelease(java.lang.Object...). This method has memory ordering effects compatible with memory_order_release ordering. Syntax: public final void s 1 min read AtomicLongArray updateAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.updateAndGet() is an inbuilt method in Java that updates the value at any given index of the AtomicLongArray after applying a given update function on the value at that index. The method takes the index value of the AtomicLongArray and the update funct 3 min read AtomicReference toString() method in Java with Examples The toString() method of a AtomicReference class is used to return the String representation of the current value of AtomicReference object. Syntax: public String toString() Parameters: This method accepts nothing. Return value: This method returns the String representation of the current value. Bel 1 min read AtomicReference setPlain() method in Java with Examples The setPlain() method of a AtomicReference class is used to set the value of this AtomicReference object with memory semantics of setting as if the variable was declared non-volatile and non-final. Syntax: public final void setPlain(V newValue) Parameters: This method accepts newValue which is the n 1 min read AtomicIntegerArray updateAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.updateAndGet() is an inbuilt method in Java that updates the value at any given index of the AtomicIntegerArray after applying a given update function on the value at that index. The method takes the index value of the AtomicIntegerArray and the upd 3 min read Like