Saturday, October 25, 2025
HomeLanguagesJavaAtomicBoolean compareAndSet() method in Java with Examples

AtomicBoolean compareAndSet() method in Java with Examples

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 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 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)

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS