Using volatile keyword with mutable object

前端 未结 5 1238
野的像风
野的像风 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:18

    Use of volatile rather than a fully synchronized value is essentially an optimization. The optimization comes from the weaker guarantees provided for a volatile value compared with a synchronized access. Premature optimmization is the root of all evil; in this case, the evil could be hard to track down because it would be in the form of race conditions and such like. So if you need to ask, you probably ought not to use it.

提交回复
热议问题