In the Java memory model, there is the main memory (main RAM) of each thread's respective thread memory (for example, registers). For performance a thread will keep a copy of the variable in its memory to access it. This will cause the same variable to occur at some point in time a thread's memory value is inconsistent with other thread memory or the value in main memory.
A variable is declared volatile, which means that the variable will be modified by other threads at any time, so it cannot be cahe in the thread memory, that is, he will not retain his copy in memory, see an example:
Public class testvolatile extends Thread { Private volatile BooleanOkay Public void Run(){ while(!okay) {System.out.println ("Current time is okay"); } }/** * Change notification variable * @description * @author Jader * May 4, 2015 PM 5:18:33 * @version 1.0 */
Public void Timetookay() {okay =true; }}
At this point the thread executes the Run method without reading its own copy of the Cahe, but instead goes back to the main memory to read, and when the Timetookay is executed, other threads are notified, and the other threads go to the main memory for the latest okay. I don't know if I understand the right thing.
Volatile does not guarantee the atomic nature of operations, unlike synchronized.
The most typical example is i++, which is actually composed of multiple atomic operations, and if multiple threads execute simultaneously i++,volatile can only guarantee the same piece of memory they operate on, For this you can use the Java.util.concurrent.atomic.AtomicInteger
The Getandincrement () method is implemented to view the API's concurrency classes under the concurrent package.
Volatile is typically used to modify variables, and synchronized is used to modify blocks of code or methods, such as:
int i1;int geti1() {return i1;}
Gets the value stored in the current thread i1, multiple threads have copies of multiple I1 variables, and these i1 values can differ. Above we have provided, multi-threaded environment in the main memory and thread memory, each thread can have their own copy of the variable, and this variable copy can be different from the main memory value, when concurrency occurs when the thread memory of I1 and thread memory I1 value is not the same
volatileint i2;int geti2(){return i2;}
The value of the i2 at this point is obtained from the main memory, and the variable modified with volatile does not allow the main memory and thread memory inconsistencies to occur. Any thread that changes the value of a variable will get the same value immediately by other threads.
int i3;synchronizedint geti3() {return i3;}
Synchronized gets and releases the monitor, and if two threads use the same object lock at the same time, the listener can force a block of code that only one thread can execute at the same time
At this point Geti3 actually go through the following steps:
1. The thread requests an object lock that monitors the This object (if it is already locked it needs to wait)
2, thread memory data is purged, read from main memory
3. Execute code block Geti3 ()
4, any changes to the variable can be safely written into the main memory
5. Thread release object lock to monitor this object
By comparison, it can be seen that volatile only synchronizes the value of a variable between the thread memory and the main memory, while synchronized is consuming more resources than volatile by locking and unlocking a monitor to synchronize all variables.
However, variables modified by the volatile keyword cannot be guaranteed to be atomic, such as i++,i–, etc.
Summarize:
1. Volatile resolves the visible problem of shared variables between threads
2, using volatile will increase the cost of performance
3. Volatile does not resolve thread synchronization issues
4, solve i++, i++ this kind of asynchronous problem need to use synchronized, Atomicinteger series of package, also increase performance overhead
The role of the volatile keyword in Java