AtomicReferenceArray weakCompareAndSet() method in Java with Examples Last Updated : 03 Jan, 2020 Comments Improve Suggest changes Like Article Like Report The weakCompareAndSet() method of a AtomicReferenceArray class is used to atomically sets the value of the element at index i to newValue to newValue for AtomicReferenceArray if the current value is equal to expectedValue passed as parameter. This method updates the value at index i with memory semantics of reading as if the variable was declared volatile type of variable. This method returns true if set a new value to AtomicReference is successful. Syntax: public final boolean weakCompareAndSet( 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 true if set a new value to AtomicReference is successful. Below programs illustrate the weakCompareAndSet() method: Program 1: Java // Java program to demonstrate // weakCompareAndSet() 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>(5); // set some value ref.set(0, 543); ref.set(1, 124); ref.set(2, 322); // apply weakCompareAndSet() boolean result = ref.weakCompareAndSet(0, 124, 234); // print value System.out.println("Setting new value" + " is successful = " + result); System.out.println("Value of index 0 = " + ref.get(0)); } } Output: Setting new value is successful = false Value of index 0 = 543 Program 2: Java // Java program to demonstrate // weakCompareAndSet() method import java.util.concurrent.atomic.*; public class GFG { public static void main(String[] args) { // create an atomic reference object.c AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(5); // set some value ref.set(0, "INDIA"); ref.set(1, "US"); ref.set(2, "UK"); ref.set(3, "CHINA"); ref.set(4, "CHINA"); // apply weakCompareAndSet() boolean result = ref.weakCompareAndSet(4, "CHINA", "CANADA"); // print value System.out.println("Setting new value" + " is successful = " + result); System.out.println("Value of index 4 = " + ref.get(4)); } } Output: Setting new value is successful = true Value of index 4 = CANADA References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#weakCompareAndSet(V, V) Comment More infoAdvertise with us Next Article AtomicReferenceArray weakCompareAndSet() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-util-concurrent-atomic package Java-AtomicReferenceArray Practice Tags : Java Similar Reads AtomicReferenceArray weakCompareAndSetPlain() method in Java with Examples The weakCompareAndSetPlain() method of a AtomicReferenceArray class is used to atomically sets the value of the element at index i to newValue to newValue for AtomicReferenceArray if the current value is equal to expectedValue passed as parameter. This method updates the value at index i with memory 2 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 AtomicReferenceArray weakCompareAndSetAcquire() method in Java with Examples The weakCompareAndSetAcquire() method of a AtomicReferenceArray class is used to atomically sets the value of the element at index i to newValue to newValue for AtomicReferenceArray if the current value is equal to expectedValue passed as parameter. This method updates the value at index i with memo 2 min read AtomicReferenceArray weakCompareAndSetVolatile() method in Java with Examples The weakCompareAndSetVolatile() method of a AtomicReferenceArray class is used to atomically sets the value of the element at index i to newValue to newValue for AtomicReferenceArray if the current value is equal to expectedValue passed as parameter. This method updates the value with memory effects 2 min read AtomicReference weakCompareAndSetPlain() method in Java with Examples The weakCompareAndSetPlain() 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 updates the value with memory semantics of setting as if the variable was declared non-v 2 min read AtomicReference weakCompareAndSetAcquire() method in Java with Examples The weakCompareAndSetAcquire() 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 updates the value with memory ordering effects compatible with memory_order_acquire or 2 min read AtomicReference weakCompareAndSetRelease() method in Java with Examples The weakCompareAndSetRelease() 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 updates the value and ensures that prior loads and stores are not reordered after this 2 min read 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 weakCompareAndSet() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.weakCompareAndSet() 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 id 2 min read AtomicReferenceArray setRelease() method in Java with Examples The setRelease() method of a AtomicReferenceArray class is used to set the value of the element at index i to newValue. Both index i and newValue are passed as parameters to the method. This method set the value with memory effects as specified by VarHandle.setRelease(java.lang.Object...). This meth 2 min read Like