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 idea if the update was done or not.
Syntax:
public final boolean weakCompareAndSet(int expect,
int val)
Parameters: The function accepts two mandatory parameters which are described below:
- expect: specifies the value that the atomic object should be.
- val: specifies the value to be updated if the atomic integer is equal to expect.
Return value: The function returns a boolean value, it returns true on success else false.
Program below demonstrates the function:
Program 1:
// Java program that demonstrates // the weakCompareAndSet() function import java.util.concurrent.atomic.AtomicInteger; public class GFG { public static void main(String args[]) { // Initially value as 0 AtomicInteger val = new AtomicInteger(0); // Prints the updated value System.out.println("Previous value: " + val); // Checks if previous value was 0 // and then updates it boolean res = val.weakCompareAndSet(0, 6); // Checks if the value was updated. if (res) System.out.println("The value was " + "updated and it is " + val); else System.out.println("The value was " + "not updated"); } } |
Previous value: 0 The value was updated and it is 6
Program 2:
// Java program that demonstrates // the weakCompareAndSet() function import java.util.concurrent.atomic.AtomicInteger; public class GFG { public static void main(String args[]) { // Initially value as 0 AtomicInteger val = new AtomicInteger(0); // Prints the updated value System.out.println("Previous value: " + val); // Checks if previous value was 0 // and then updates it boolean res = val.weakCompareAndSet(10, 6); // Checks if the value was updated. if (res) System.out.println("The value was " + "updated and it is " + val); else System.out.println("The value was " + "not updated"); } } |
Previous value: 0 The value was not updated
Recommended Posts:
- AtomicLong weakCompareAndSet() method in Java with examples
- AtomicInteger get() method in Java with examples
- AtomicInteger set() method in Java with examples
- AtomicInteger accumulateAndGet() method in Java with Examples
- AtomicInteger longValue() method in Java with examples
- AtomicInteger lazySet() method in Java with examples
- AtomicInteger toString() method in Java with examples
- AtomicInteger decrementAndGet() method in Java with examples
- AtomicInteger updateAndGet() method in Java with Examples
- AtomicInteger compareAndSet() method in Java with examples
- AtomicInteger getAndAccumulate() method in Java with Examples
- AtomicInteger addAndGet() method in Java with examples
- AtomicInteger getAndIncrement() method in Java with examples
- AtomicInteger incrementAndGet() method in Java with examples
- AtomicInteger doubleValue() method in Java with examples
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.



