Java 锁定对象是否强制执行事前发生关系?

Does a Java Lock object enforce a happens-before relationship?

Java 根据文档 provides more extensive locking operations than can be obtained using synchronized methods and statements.

在并发包中提供了一个 Lock 对象

同步 methods/blocks 除了互斥之外,还强制执行一种事前发生关系,确保一个线程对变量所做的更改对另一个线程可见。

使用Lock对象时会出现这种关系吗?是否像所有平台的同步块一样保证观察?

是的,确实如此。

Lock objects work very much like the implicit locks used by synchronized code. As with implicit locks, only one thread can own a Lock object at a time. Lock objects also support a wait/notify mechanism, through their associated Condition objects.

来自https://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html

Does this relationship occur when using a Lock object? Is observation guaranteed like in the case of synchronized block for all platforms?

是的,确实如此。

有几种操作可以创建先行关系,其中之一是同步 (here),Java 的锁定对象也用于此目的。

从 Oracle 文档中阅读有关 Java 的 Memory Consistency Properties 的信息。除了下面将在 link 中突出显示的内容。

下面的“扩展这些保证”表示内存一致性属性,如 "happens-before" 关系。 Lock class 属于 java.util.concurrent 的子包,因此它保证了 "happens-before" 关系等内存一致性属性。

The methods of all classes in java.util.concurrent and its subpackages extend these guarantees to higher-level synchronization.