Why aren't variables in Java volatile by default?

后端 未结 6 1654
夕颜
夕颜 2020-12-30 07:05

Possibly similar question:

Do you ever use the volatile keyword in Java?


Today I was debugging my game; It had a very difficult threading problem that w
6条回答
  •  误落风尘
    2020-12-30 07:24

    Because the compiler can't optimise volatile variables.

    volatile tells the compiler that the variable can change at any time. Therefore, it can't assume that the variable won't change and optimise accordingly.

提交回复
热议问题