read a section of the concurrent programming network http://ifeve.com/volatile/
Volatile is a lightweight synchronized, that ensures the" visibility "of shared variables in multiprocessor development . Visibility means that when one thread modifies a shared variable, another thread can read the modified value.
in order to improve the processing speed, the processor does not communicate directly with memory, but first reads the data in the system memory into the internal cache (L1, L2 or other) and then operates, but after the operation, it does not know when it will be written to memory. If the Volatile variable is declared for write operation, JVM will send a Lock prefix instruction to the processor to write the data of the cache line of this variable back to system memory. But even if you write back to memory, if the cache values of other processors are still old, there will be problems in performing computing operations, so under multiprocessors, in order to ensure that the caches of each processor are consistent, a cache consistency protocol will be implemented. Each processor checks whether its own cached value is expired by sniffing data propagated on the bus. When the processor finds that the memory address of its cache line has been modified, it will set the cache line of the current processor to an invalid state, and when the processor wants to modify this data, will force the data to be re-read from the system memory into the processor cache.
my question is whether caching is visible to multithreading under a single processor?! A single processor doesn"t need to think about visibility?