数组读/写本质上是原子的吗?
int[] arr = new int[10];
int[8] = 4; // This is what I'm interested in
在上面的代码中,第二行应该生成两个机器指令,例如
reg1 = reg_containing_arr_address + 32
Memcopy reg1 4
一些posts在堆栈溢出上看到这些是原子的。谁能解释一下他们怎么样?
最佳答案
The components (elements) of arrays are variables.
The result of an array access expression is a variable of type
T
, namely the variable within the array selected by the value of the index expression.
根据this answer ,int
变量的写入和读取是原子的。由于您的数组是 int 类型,因此对其的读取和写入是原子的。
关于Java 数组写/读操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28773092/