This article mainly introduces the relevant information about the difference and connection between volatile and synchronized in Java. I hope this article can help everyone understand this part of the content. Friends in need can refer to it
The difference and connection between volatile and synchronized in java
This may be the best article comparing the effects of volatile and synchronized. Volatile is a variable modifier, and synchronized is a method or block modifier. So we use these two keywords to specify three simple ways to access variables
int i1; int geti1() {return i1;} volatile int i2; int geti2() {return i2;} int i3; synchronized int geti3() {return i3;}
geti1() immediately obtains the i1 variable in the current thread value in . A thread can obtain a local copy of a variable, and the value of the variable obtained is not necessarily the same as the value obtained by other threads. In particular, if other threads modify the value of i1, the value of i1 obtained by the current thread may be different from the modified value. In fact, Java has a main memory mechanism that uses a main memory to save the current correct value of the variable. Threads copy the values of variables to their own independent memory, and the memory copies of these threads may be different from the values in main memory. So in practice, it may happen that the value of i1 in the main memory is 1, and both thread 1 and thread 2 have changed i1, but the updated value has not been transferred back to the main memory or other threads. Then it may be that in the thread The value of i1 in thread 1 is 2, but the value of i1 in thread 2 is 3.
On the other hand, geti2() can effectively obtain the value of i2 from main memory. A volatile variable does not allow a thread to copy the value of the variable from main memory to its own storage space. Therefore, a variable declared as volatile will obtain data synchronously in all threads. No matter you change the variable in any thread, other threads will immediately get the same result. Because it is more efficient for threads to access or change their own data copies, volatile type variables consume some performance.
So if volatile variables can already synchronize data between threads, what are synchronizes used for? There are two differences between the two. First, synchronized acquires and releases locks controlled by the listener. If both threads use a listener (that is, the same object lock), then the listener can force only one thread to process the code block at a time. This is the most general Synchronize. In addition, synchronized can also synchronize memory. In practice, synchronized makes all thread memory synchronized with main memory. So the execution process of geti3() is as follows:
1. The thread acquires the object's lock from the listener. (It is assumed here that the listener is not locked, otherwise the thread cannot obtain the object lock until the listener is unlocked)
2. The thread memory updates all variables, which means that it will read the variables in the main memory Make your own variables guaranteed to be valid. (The JVM will use a "dirty" flag to optimize the process so that only variables with the "dirty" flag are updated. For details, check 17.9 of the JAVA specification)
3. The code block is executed ( In this example, set the return value to the current value of i3 just reset from main memory. )
4. Any changes to the variables will be written back to main memory. But there is no change in geti3() in this example.
5. The thread releases the object's lock to the listener.
So volatile can only synchronize the value of one variable between thread memory and main memory, while synchronized synchronizes the value of all variables between thread memory and main memory, and by locking and Release the listener to achieve this. Obviously, synchronized will be more expensive in performance than volatile.
About the difference between the two
1. The essence of volatile is to tell the jvm that the value of the current variable in the register (working memory) is uncertain. It needs to be read from main memory; synchronized locks the current variable. Only the current thread can access the variable, and other threads are blocked.
2.volatile can only be used at the variable level; synchronized can be used at the variable, method, and class levels
3.volatile can only be implemented The modification visibility of variables cannot guarantee atomicity; while synchronized can guarantee the modification visibility and atomicity of variables
4.volatile will not cause thread blocking; synchronized may cause Thread blocking.
5. Variables marked volatile will not be optimized by the compiler; variables marked synchronized can be optimized by the compiler
The reasons for the red font part are as follows:
When thread A modifies the variable before it ends, another thread B can see the modified value and can modify the variable without waiting for A to release the lock, because the Volatile variable Unlocked
The above is the detailed content of The difference and connection between synchronized and volatile in Java. For more information, please follow other related articles on the PHP Chinese website!