Volatile piggyback. Is this enough for visiblity?
This is about volatile piggyback. Purpose: I want to reach a lightweight vars visibilty. Consistency of a_b_c is not important. I have a bunch of vars and I don't want to make them all volatile. Is this code threadsafe? class A { public int a, b, c; volatile int sync; public void setup() { a = 2; b = 3; c = 4; } public void sync() { sync++; } } final static A aaa = new A(); Thread0: aaa.setup(); end Thread1: for(;;) {aaa.sync(); logic with aaa.a, aaa.b, aaa.c} Thread2: for(;;) {aaa.sync(); logic with aaa.a, aaa.b, aaa.c} Adam Zalcman Java Memory Model defines the happens-before relationship