Java AtomicInteger weakCompareAndSet() method

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

Test it Now

Output:

the  new value is: 100

Example 2

Test it Now

Output:

the  new value is: 1

Example 3

Test it Now

Output:

the  new value is: 10

Example 4

Test it Now

Output:

the  new value is: 1

Example 5

Test it Now

Output:

the  new value is: -100





Latest Courses