Java AtomicInteger weakCompareAndSet() 方法

2025 年 3 月 20 日 | 阅读 2 分钟

Java AtomicInteger 类的 weakCompareAndSet() 方法以原子方式将当前值设置为新值,如果当前值等于预期值。

语法

参数

此方法接受两个参数:一个预期值,另一个是新值。

返回

如果当前值和预期值相等,则返回新值。

示例 1

输出

the  new value is: 100

示例 2

输出

the  new value is: 1

示例 3

输出

the  new value is: 10

示例 4

输出

the  new value is: 1

示例 5

输出

the  new value is: -100
 
下一主题Java AtomicInteger