Get it from: http://space.itpub.net/111631/viewspace-610344
What is the volatile keyword used?
I'm afraid it is easiest to clarify the differences between volatile and synchronized. Volatile is a variable modifier, while synchronized acts onCodeOr method; see the following three get codes:
Int I1; int geti1 () {return I1 ;}
Volatile int I2; int geti2 () {return I2 ;}
Int I3; synchronized int geti3 () {return I3 ;}
geti1 () obtains the i1 value stored in the current thread. Multiple Threads have multiple I1 variable copies, and these i1. In other words, another thread may have changed the i1 value in its thread, which may be different from the i1 value in the current thread. In fact, Java has a concept called "primary" memory region, where the current "exact value" of the variable is stored ". Each thread can copy its own variables, and the copy value of this variable can be different from that stored in the "Main" memory area. Therefore, there is a possibility that the i1 value in the "Main" memory area is 1, and the i1 value in thread 1 is 2, the I1 value in thread 2 is 3 -- both thread 1 and thread 2 change their I1 values, this change will happen before being passed to the "Main" memory area or other threads.
geti2 () obtains the i2 value of the "master" memory area. Variables modified with volatile are not allowed to be copied in a variable different from the "primary" memory area. In other words, a variable must be synchronized in all threads after being modified by volatile. Any thread changes its value and all other threads get the same value immediately. Naturally, volatile variable access requires a little more resources than normal variables consume, because the thread has its own variable copy efficiency.
now that the volatile keyword has been used to synchronize data between threads, what should we do with synchronized? There are two differences between them. First, synchronized acquires and releases the monitor-if two threads use the same object lock, the monitor can force the code block to be executed by only one thread at the same time-this is a well-known fact. However, synchronized also synchronizes the memory: in fact, synchronized synchronizes the memory of the entire thread in the "master" memory area. Therefore, the following steps are taken to execute the geti3 () method:
1. the thread requests to obtain the object lock that monitors this object (assuming it is not locked, otherwise the thread waits until the lock is released)
2. data in the thread memory is eliminated and read from the "master" memory area (this step can be optimized by the Java Virtual Machine... [Do not know how to express it later, Khan])
3. code block executed
4. any change to the variable can now be safely written to the "master" memory area (but the geti3 () method does not change the variable value)
5. the object lock for thread release monitoring this object
so volatile only synchronizes the value of a variable between the thread memory and the "Main" memory, synchronized synchronizes the values of all variables by locking and unlocking a monitor. Obviously, synchronized consumes more resources than volatile.