AtomicBoolean compareAndSet() method in Java with Examples Last Updated : 27 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The java.util.concurrent.atomic.AtomicBoolean.compareAndSet() 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 compareAndSet(boolean expect, boolean val) Parameters: The function accepts two mandatory parameters which are described below: expect: which specifies the value that the atomic object should be. val: which specifies the value to be updated if the atomic Boolean is equal to expect. Return Value: The function returns a boolean value, it returns true on success else false. Below programs illustrate the above function: Program 1: Java // Java Program to demonstrates // the compareAndSet() function import java.util.concurrent.atomic.AtomicBoolean; public class GFG { public static void main(String args[]) { // Initially value as false AtomicBoolean val = new AtomicBoolean(false); // Prints the updated value System.out.println("Previous value: " + val); // Checks if previous value was false // and then updates it boolean res = val.compareAndSet(false, true); // 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"); } } Output: Previous value: false The value was updated and it is true Program 2: Java // Java Program to demonstrates // the compareAndSet() function import java.util.concurrent.atomic.AtomicBoolean; public class GFG { public static void main(String args[]) { // Initially value as true AtomicBoolean val = new AtomicBoolean(true); // Prints the updated value System.out.println("Previous value: " + val); // Checks if previous value was true // and then updates it boolean res = val.compareAndSet(true, false); // 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"); } } Output: Previous value: true The value was updated and it is false Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicBoolean.html#compareAndSet(boolean, %20boolean) Comment More infoAdvertise with us Next Article AtomicReference compareAndSet() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-Functions Java-concurrent-package Java-AtomicBoolean Practice Tags : Java Similar Reads AtomicLong compareAndSet() method in Java with examples The Java.util.concurrent.atomic.AtomicLong.compareAndSet() 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 t 2 min read AtomicInteger compareAndSet() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.compareAndSet() 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 i 2 min read AtomicReference compareAndSet() method in Java with Examples The compareAndSet() method of a AtomicReference class is used to Atomically sets the value to newValue to AtomicReference object, if the current value of AtomicReference object equal to the expectedValue and returns the true if operation is successful else return false. This method updates the value 2 min read AtomicLongArray compareAndSet() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.compareAndSet() is an inbuilt method in java that atomically sets the element at a position to the given updated value if the current value is equal to the expected value. This method takes the index value, the expected value and the update value as th 3 min read Boolean compareTo() method in Java with examples The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: BooleanObject.compareTo(Boolean a) Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance. Return 2 min read Boolean compare() method in Java with Examples The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name. Syntax: Boolean.compare(boolean a, boolean b) Parameters: It 2 min read Like