Volatile is a variable modifier, while synchronized acts on a code or method; The following three-sentence get code:
1 intI1;2 intGeti1 () {returnI1;}3 4 volatile intI2;5 intGeti2 () {returnI2;}6 7 inti3;8 synchronized intGeti3 () {returni3;}
Geti1 () Gets the value i1 stored in the current thread. Multiple threads have multiple copies of I1 variables, and these i1 can differ from each other. In other words, another thread may have changed the I1 value within its thread, and this value can be different from the I1 value in the current thread. In the Java memory model, there is the main memory area, where the variable's current "exact value" is stored, and each thread has its own memory (for example, a register). For performance, a thread saves a copy of the variable to be accessed in its own memory. This will cause the same variable to appear in an instant, where the value in the memory of one thread may be inconsistent with the value of another thread's memory, or the value of main memory. So there is actually a possibility: main memory value I1 value is 1, thread 1 in I1 is 2, thread 2 I1 value is 3, this thread 1 and 2 have changed their respective i1 values, and this change has not been passed to the main memory or other threads will occur.
Geti2 () Gets the i2 value of the main memory. a variable declared as volatile means that the variable is modified by other threads at any time, so it cannot be cache in the thread memory. in other words, a variable is volatile and must be synchronous in all threads, changing its value in any thread, and all other threads immediately get the same value. As a result, volatile-modified variables are accessed more often than normal variables because the thread has its own variable copy that is more efficient.
The Geti3 () method is synchronized decorated with synchronized to modify a method or a loan to ensure that at most one thread executes the code at the same time. Now that the volatile keyword has implemented data synchronization between threads, what's the synchronized to do? When two concurrent threads access the same object, the synchronized (this) synchronizes the loan, only one thread can be executed within a single time. Another thread must wait for the current thread to execute this block of code before executing the code block. However, when a thread accesses one synchronized (this) of object to synchronize a block of code, another thread can still access the non-synchronized (this) synchronous code block in that object. In particular, when a thread accesses one of the synchronized (this) synchronization blocks of object, other threads will block access to all other synchronized (this) synchronous blocks of code in object. When a thread accesses a synchronized (this) of object to synchronize a block of code, it obtains the object lock for that object and, as a result, the other threads are temporarily blocked from accessing all of the synchronized code portions of the objects.
Summarize the difference:
One, volatile is a variable modifier, while synchronized acts on a code or method.
Second, volatile is simply synchronizing the value of a variable between the thread memory and main memory, while synchronized synchronizes the values of all variables by locking and unlocking a monitor. Obviously synchronized consumes more resources than volatile.
The role and difference of keywords volatile and synchronized in Java