Java volatile reference vs. AtomicReference

后端 未结 6 1085
慢半拍i
慢半拍i 2020-11-28 02:06

Is there any difference between a volatile Object reference and AtomicReference in case I would just use get() and set()-

6条回答
  •  甜味超标
    2020-11-28 03:03

    Short answer is: No.

    From the java.util.concurrent.atomic package documentation. To quote:

    The memory effects for accesses and updates of atomics generally follow the rules for volatiles:

    • get has the memory effects of reading a volatile variable.
    • set has the memory effects of writing (assigning) a volatile variable.

    By the way, that documentation is very good and everything is explained.


    AtomicReference::lazySet is a newer (Java 6+) operation introduced that has semantics unachievable through volatile variables. See this post for more information.

提交回复
热议问题