Language syntax
Happens-before
Reason about ordered memory actions, including start, termination, monitor and volatile edges.
Formal Syntax & Structure
java syntaxjava
Conceptual relation: A happens-before BWorking Example
Example Snippetjava
import java.time.Duration;import java.util.concurrent.*;public class Main { // Only finite tasks and timed coordination are used in these examples. static void finish(Thread thread) throws InterruptedException { try { if (!thread.join(Duration.ofSeconds(3))) { thread.interrupt(); if (!thread.join(Duration.ofSeconds(3))) throw new AssertionError("worker did not stop"); throw new AssertionError("worker timed out"); } } catch (InterruptedException e) { thread.interrupt(); throw e; } } static class Box { int input; int result; } public static void main(String[] args) throws Exception { Box box = new Box(); box.input = 21; Thread worker = new Thread(() -> box.result = box.input * 2); worker.start(); finish(worker); System.out.println(box.result); }}