美文网首页
CAS (Compare And Swap or Compare

CAS (Compare And Swap or Compare

作者: 填坑之路_DK | 来源:发表于2021-04-01 11:26 被阅读0次
    • Compare And Swap 比较并交换 (读取 > 修改 > CAS 拿之前读取值和现存值一致时执行交换)
    • java实现类 Unsafe ,解释为 Compare And Set
    • 原子操作 cpu底层支持
    • Compare不通过时,自旋 重新执行 读取 > ... (while true 实现)
    • ABA 问题 (读取为0 交换时也为0 但是在 读取 -> 交换的过程中 其他线程修改为 N 再 修改为 0 ) 类似乐观锁 读取记录标识 修改更改标识 jdk有默认实现
    • cas 对应汇编代码 lock cmpxchg (首先会判断是否是多个cpu 多个cpu会加锁)

    相关文章

      网友评论

          本文标题:CAS (Compare And Swap or Compare

          本文链接:https://www.haomeiwen.com/subject/tmmbhltx.html