java - java变量没有volatile,线程修改cpuA中的变量切换到cpuB时一致性如何
问题描述
在一个线程中尝试此代码块
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
如果 x 和 y 是同一线程的操作,并且 x 在程序顺序中位于 y 之前,则为 hb(x, y)。
推荐阅读
- r - 查找时间戳向量更改时区的位置
- javascript - 如果可能的话,我需要一个匹配以下内容的正则表达式,以及如何使用 javascript 或 node js 包对其进行测试
- r - 无法在箱线图中设置轴的间隔:RStudio
- matlab - 绘制区域邻接图
- sas - 过程 SQL SAS 基本
- angular - 端点返回错误,但 http 订阅没有捕获它
- android - fs-extra:源和目标不能相同。(科尔多瓦)
- xamarin.android - 无法使用 Xamarin Android/Uwp 在 USB 闪存驱动器中写入文件
- javascript - WordPress:在 JavaScript 中显示 null 的 PHP 变量
- css - 我如何切换 wordpress 子菜单项隐藏/显示