Yes, or something that has the same guarantees, such as accessing inside a synchronized block (on both threads, on the same lock!) or getting more complicated, the various other locks and atomic things in
java.util.concurrent
(eg. there is an AtomicBoolean that can atomically compare and set)
Grand, I think I can manage it with volatile for now anyway.
f you have a situation where two threads are reading / writing to the same variable and each thread doesn’t care about the other, you probably shouldn’t be using a shared variable in the first place!
Yeah, I suppose I can’t think of anything where this is the case - I guess I was thinking about something where the variable was being read for some less important reason that didn’t require it to be fully synced with the timing of the other thread. But obviously it still does cares.
Anyway, thanks for all the help. I hadn’t come across volatile variables before so it’s great to understand them (somewhat).