How to demonstrate Java instruction reordering problems?

后端 未结 3 425
借酒劲吻你
借酒劲吻你 2021-01-31 02:47

With Java instruction reordering the execution order of the code is changed by the JVM at compile time or run time, possibly causing unrelated statements to be executed out-of-o

3条回答
  •  离开以前
    2021-01-31 03:21

    For single thread executions reordering is not a problem at all, because of Java Memory Model (JMM) (guarantee that any reads actions related to writes are total ordered) and cannot lead to unexpected results.

    For concurrent execution, rules are completely different and things gets more complicated to understand (even by providing simple example which will raise even more questions). But even this are totally described by JMM with all corner cases, so, unexpected results also forbidden. Generally, forbidden if all barriers are placed right.

    For better understanding reordering I strongly recommend this subject with plenty examples insides.

提交回复
热议问题