Volatile: why prevent compiler reorder code
问题 In java, with my knowledge, volatile variable make a thread reads/writes directly to main CPU (not in cache of each thread), so make its change visibles to other threads. The thing I don't know is : So, why this work (of volatile) can prevent compiler/CPU reorder statement of code. thanks :) 回答1: Here is a very good example illustrating the issue the prohibition on reordering is aimed to address (taken from here): class VolatileExample { int x = 0; volatile boolean v = false; public void