Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardJavavolatile semanticsSingle-choice MCQ

The program below has no data race and a happens-before edge through the volatile flag. After Thread 2 observes flag == true, what is guaranteed about r1?

int data = 0; volatile boolean flag = false; // Thread 1 // Thread 2 data = 42; while (!flag) {} flag = true; int r1 = data;