分类: Java
2010-07-05 11:11:54
Hist:
100705, draft
当不同的thread访问同一个memory时,thread看到的的memory不一致(有差异), 这就是Memory Consistency Error. 也就是说thread A改变memory后,并不保证thread B能即刻看到这些变化. 实际就是同步的问题.
要避免Memory Consistency Error, 就必须要求thread A的变动memory操作必须比thread B的观察操作’发生的早’(happens-before). Happens-before 在JLS 17.4.5()中有说明, 引用如下:
Two actions can be ordered by a happens-before relationship. If one action happens-before another, then the first is visible to and ordered before the second.
If we have two actions x and y, we write hb(x, y) to indicate that x happens-before y.
It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results consistent with a legal execution, it is not illegal.