Java AtomicLong compareAndSet() Method

The compareAndSet() method of java AtomicLong class compares the current value of the AtomicLong instance to an expected value and if the two values are equal, it sets a new value for the AtomicLong instance.

Syntax

Parameters

expectedValue - The expected value

newValue - The new value

Example 1

Test it Now

Output:

111

Example 2

Test it Now

Output:

222

Example 3

Test it Now

Output:

-1

Example 4

Test it Now

Output:

-10

Next TopicJava AtomicLong




Latest Courses