问题描述
在一个线程中尝试此代码块
int a = 0;
a = 1; //the thread is running on cpu A
a? //the thread is switched and running on cpu B
所以a可以是0吗?
解决方法
否,a
必须为1,因为赋值a = 1
与a
的值之间有一个happens-before relationship,该值稍后由同一线程读取。
如果x和y是同一线程的动作,并且x按程序顺序位于y之前,则hb(x,y)。