Reference assignment is atomic so why use AtomicReference

前端 未结 3 921
闹比i
闹比i 2020-12-29 22:38

I have simple general question about AtomicReference.

Why use AtomicReference if reference assignment is atomic in java?

Also I would like to ask if referen

3条回答
  •  梦毁少年i
    2020-12-29 22:58

    The reason is that even though the reference is atomic, it is atomic in a very narrow sense.

    If a thread writes a non volatile reference, what is guaranteed is that other threads will see the whole write or not see it at all (no word tearing/garbage).

    But at no point it is guaranteed that any other thread will ever see it nor that they will be seen in the same order.

    An AtomicReference provides much stronger guarantees (besides the CAS operations), essentially they behave like volatile:

    • Any writes that happened in thread A before a volatile write are visible in thread B after a subsequent volatile read of that variable
    • volatile operations cannot be reordered

提交回复
热议问题