Friday, July 24, 2026
HomeLanguagesJavaAtomicLong weakCompareAndSet() method in Java with examples

AtomicLong weakCompareAndSet() method in Java with examples

The Java.util.concurrent.atomic.AtomicLong.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(long expect,
                                       long 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 longer is equal to expect.

Return value: The function returns a boolean value, it returns true on success else false. 

Below programs illustrate the above method:

Program 1:  

Java




// Java program that demonstrates
// the weakCompareAndSet() function
 
import java.util.concurrent.atomic.AtomicLong;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Initially value as 0
        AtomicLong val = new AtomicLong(0);
 
        // Prlongs 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");
    }
}


Output: 

Previous value: 0
The value was updated and it is 6

 

Program 2: 

Java




// Java program that demonstrates
// the weakCompareAndSet() function
 
import java.util.concurrent.atomic.AtomicLong;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Initially value as 0
        AtomicLong val = new AtomicLong(0);
 
        // Prlongs 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");
    }
}


Output: 

Previous value: 0
The value was not updated

 

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLong.html#weakCompareAndSet–
 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS