Does Java reordering affect System.currentTimeMillis()?

后端 未结 2 526
温柔的废话
温柔的废话 2021-02-18 23:19

According to Java Memory Model, instructions can be reordered as long as the execution is well-formed.

So I wonder, is it possible that the following codes produces the

2条回答
  •  囚心锁ツ
    2021-02-18 23:52

    Please see this question Instruction reordering & happens-before relationship in java.

    I believe that unless you are in a different thread, the outcome of any execution will always be consistent with the order in your code. In this situation, since it is impossible to process it out of order, it should be good even if your fields are visible to another thread.

提交回复
热议问题