Skip to content
Discussion options

You must be logged in to vote

Guarantees visibility + ordering between threads in Java Memory Model.

  1. Thread start

int x = 10;
new Thread(() -> {
System.out.println(x); // always sees 10
}).start();

➡️ start() happens-before thread execution

  1. Thread join

int x = 0;
Thread t = new Thread(() -> x = 5);
t.start();
t.join();
System.out.println(x); // always 5

➡️ thread completion happens-before join()

  1. Volatile

volatile boolean flag = false;

Thread 1: flag = true;
Thread 2: if(flag) { /* guaranteed visible */ }

➡️ write → read has happens-before

  1. Synchronization

synchronized(lock) { x = 10; } // Thread 1
synchronized(lock) { print(x); } // Thread 2 → sees 10

➡️ unlock happens-before next lock

  1. Atomic (CAS)

AtomicInte…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant