Happens-before rules in Java Memory Model

前端 未结 5 783
迷失自我
迷失自我 2021-01-18 10:21

I am currently studying for a concurrent programming exam and don\'t understand why the output of this program is 43. Why is x = y + 1 executed before t.s

5条回答
  •  旧时难觅i
    2021-01-18 11:08

    There is no synchronization, no volatile fields, no locking, no atomic fields. The code can execute in any order.

    Yes, t.start() will execute before x = y + 1. But starting the thread doesn't mean the thread body executes before x = y + 1. It could run before, or after, or interleaved with the rest of main().

提交回复
热议问题