回答Does a correctly synchronized program still allow data race?(Part I)时,它为我们提供了一个很好的例子:程序的所有执行似乎顺序一致,但是仍然存在数据争用.它告诉我们为什么JLS中的结论的另一个方向不正确:
If a program has no data races, then all executions of the program will appear to be sequentially consistent.
现在看一下JLS中的另一个结论:
A program is correctly synchronized if and only if all sequentially consistent executions are free of data races.
根据这个结论,上面的示例未正确同步,那么正确的程序是否会错误同步?
最佳答案
您可能首先需要定义什么是正确的程序(不容易). JCiP建议(在不同的上下文中):
a program is correct if it conforms to its specifications.
使用该定义,提供的示例是正确的.但是,它没有正确同步(哈希上存在数据争用).
==>如该示例所证明的,正确的程序可能被错误地同步.