java理解与同步
我试图理解Java happens-before order的概念,但有几件事似乎非常令人困惑。据我所知,before只是一个操作集上的命令,不提供任何关于实时执行顺序的保证。实际上(强调我的):
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.
所以,它所说的是,如果有两个动作w
(写)和r
(读)使得^{em>hb(w,r),那么r
可能在执行中发生在w
之前,但不能保证它会发生。此外,写入w
由读取r
观察
如何确定在运行时随后执行两个操作?例如:
public volatile int v;
public int c;
行动:
Thread A
v = 3; //w
Thread B
c = v; //r
这里我们有hb(w, r)
,但这并不意味着c
在赋值后将包含值3
。我如何强制c
分配3?synchronization order是否提供了此类保障
共 (0) 个答案