Happens-before rules in Java Memory Model

前端 未结 5 686
迷失自我
迷失自我 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条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 11:00

    t.start is before x = y + 1, it does not gurantee every line of code in run() method will be executed before x = y + 1.

    In fact, the printed result is uncertain without syncronization because of race condition, it could be 1 or 43.

提交回复
热议问题