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

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


Yes, it does.

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.

From 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?

Yes, it does.

There are several actions that create happens-before relationships and one of them is synchronization (here), and Java's lock object is also meant for that purpose.

Read about Java's Memory Consistency Properties from Oracle docs. Except below which will be highlight in the link.

In below "extend these guarantees" means memory consistency properties like "happens-before" relationships. Lock class belongs to subpackage of java.util.concurrent, so it guarantees memory consistency properties like "happens-before" relationships and more.

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