Using volatile keyword with mutable object

前端 未结 5 1230
野的像风
野的像风 2020-12-05 06:47

In Java, I understand that volatile keyword provides visibility to variables. The question is, if a variable is a reference to a mutable object, does vola

5条回答
  •  一向
    一向 (楼主)
    2020-12-05 07:34

    volatile does not "provide visibility". Its only effect is to prevent processor caching of the variable, thus providing a happens-before relation on concurrent reads and writes. It does not affect the members of an object, nor does it provide any synchronisation synchronized locking.

    As you haven't told us what the "correct" behaviour of your code is, the question cannot be answered.

提交回复
热议问题