java - 正确的程序可能会被错误地同步吗?

标签 java memory-model

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/

相关文章:

java - getAdapterPosition 返回 NO_POSITION,返回实际值(适配器位置 - 1)

java - assertJ findFrame() 失败并出现 NoClassDefFoundError

java - 将 2D ArrayList<String> 转换为 2D 字符串数组

java - Java 中 Apache FOP 的 XSL 错误

c# - 绑定(bind) View 模型属性在后台线程中更新; UI 会始终看到更新后的值吗?

c++ - 关于 shared_ptr 析构函数中实现错误的困惑

java - 如何使用子查询.in

java - 为什么变量在没有同步的情况下对其他线程可见?

c++ - 是否可以直接访问线程共享变量?

.net - .NET 上 x86/x64/ia64 内存模型之间的差异