Boolean not changing in thread

前端 未结 1 1199
难免孤独
难免孤独 2021-01-21 16:29

I have a class MPClient and MultiplayerMatch. MultiplayerMatch, in his constructor, creates a MPClient runnable thread.

To avoid data overflow, I have a boolean named \"

1条回答
  •  鱼传尺愫
    2021-01-21 17:03

    This is because it is reading a cached value of match.moved variable instead of the latest. To avoid this, declare the variable as volatile

    public volatile boolean moved;
    

    Read more here

    0 讨论(0)
提交回复
热议问题