Java Concurrency - Happens-before Relationship

> Procedural Languages > Java > Java - Concurrency (Parallel Work)

1 - About

The happens-before relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement.

The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation.

And so to avoid a memory consistency problem.


3 - Actions

The following actions create happens-before relationships:

  • synchronized construct. When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquisition of the same lock.
  • volatile constructs
  • Thread.start: Every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the creation of the new thread are visible to the new thread.
  • Thread.join. All the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join. The effects of the code in the thread are now visible to the thread that performed the join.

For a list of actions that create happens-before relationships, refer to the Summary page of the java.util.concurrent package..

4 - Documentation / Reference