Java AtomicInteger compareAndSet() methodThe compareAndSet() method of Java AtomicInteger class compares the current value with the expected value. If both the values are same, it will update the value. SyntaxParameterexpect: It is the expected value update: It is the new value ReturnThis method returns the new value as an atomicInteger if the expected value is equal to the current value. Example 1Test it NowOutput: the updated number: 444 Example 2Test it NowOutput: the updated number:222 In the above example, the expected value is not equal to the newValue so, the value is not updated and it remains as intial value. Example 3Test it NowOutput: the updated number: 0 Example 4Test it NowOutput: the updated number: 100 Example 5Test it NowOutput: the updated number: -2 Next TopicJava AtomicInteger |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India