The volatile key word and memory consistency errors

前端 未结 6 2009
借酒劲吻你
借酒劲吻你 2021-02-09 11:20

In the oracle Java documentation located here, the following is said:

Atomic actions cannot be interleaved, so they can be used without f

6条回答
  •  别跟我提以往
    2021-02-09 11:49

    when coming to concurrency, you might want to ensure 2 things:

    • atomic operations: a set of operations is atomic - this is usually achieved with "synchronized" (higher level constructs). Also with volatile for instance for read/write on long and double.

    • visibility: a thread B sees a modification made by a thread A. Even if an operation is atomic, like a write to an int variable, a second thread can still see a non-up-to-date value of the variable, due to processor caches. Putting a variable as volatile ensures that the second thread does see the up-to-date value of that variable. More than that, it ensures that the second thread sees an up-to-date value of ALL the variables written by the first thread before the write to the volatile variable.

提交回复
热议问题