Java AtomicInteger weakCompareAndSet() method

20 Mar 2025 | 2 min read

The weakCompareAndSet() method of Java AtomicInteger class atomically sets the value to the new value if current value is equal to the expected value.

Syntax

Parameters

This method will take the two arguments one is expected value and other one is new value.

Return

It will return the new value if the current value and the expected value are equal.

Example 1

Output:

the  new value is: 100

Example 2

Output:

the  new value is: 1

Example 3

Output:

the  new value is: 10

Example 4

Output:

the  new value is: 1

Example 5

Output:

the  new value is: -100