Why are two AtomicIntegers never equal?

前端 未结 9 2135
日久生厌
日久生厌 2020-12-01 18:02

I stumbled across the source of AtomicInteger and realized that

new AtomicInteger(0).equals(new AtomicInteger(0))

evaluates to

9条回答
  •  时光取名叫无心
    2020-12-01 18:16

    I suspect that comparing the values is a no-go since there's no way to do it atomically in a portable fashion (without locks, that is).

    And if there's no atomicity then the variables could compare equal even they never contained the same value at the same time (e.g. if a changed from 0 to 1 at exactly the same time as b changed from 1 to 0).

提交回复
热议问题