在 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.
使用该定义,所提供的示例是正确的。但是,它没有正确同步(hash
上存在数据竞争)。
==> 正如该示例所证明的那样,正确的程序可能会被错误地同步。
关于java - 正确的程序可能会被错误地同步吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12073197/