Does Java reordering affect System.currentTimeMillis()?

后端 未结 2 528
温柔的废话
温柔的废话 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:45

    Due to being a user system call, compilers shouldn't reorder them in the same thread. If this was not true, we could even experience reordering effects in System.out.println(independent values); I guess that access to the System's/OS's clock creates a sort of relationship between these operations (always for the current thread), so theoretically there is some kind of dependency between them. Probably, JVM considers this issue and never reorders User System calls.

提交回复
热议问题