Differences and relationships between volatile and synchronized []

Source: Internet
Author: User

Volatile is a variable modifier, while synchronized is a method or block modifier. Therefore, 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 () inCurrent threadGet the value in the i1 variable immediately. The thread can obtain the local copy of the variable, and the obtained variable value is not necessarily the same as the value obtained by other threads. In particular, if other threads modify the i1 value, the i1 value obtained by the current thread may be different from the modified value. In fact, Java has a primary memory mechanism that uses a primary memory to save the current correct value of the variable. The thread copies the value of the variable to its own independent memory, and the memory copy of these threads may be different from the value in the main memory. So this may happen in reality. In the main memory, the i1 value is 1, and both thread 1 and thread 2 Change I1, however, the updated value is not returned to the main memory or other threads. Therefore, the i1 value in thread 1 may be 2, but the i1 value in thread 2 may be 3.

On the other hand, geti2 () can effectively obtain the i2 value from the main memory. A volatile variable does not allow the thread to copy the value of the variable from the main memory to its own bucket. Therefore, a variable declared as volatile will get 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 modify their own data copies, volatile variables consume performance.

So if the volatile variable can synchronize data between threads, What Is synchronizes used? There are two differences between the two. First, synchronized acquires and releases the lock controlled by the listener. If both threads use one listener (that is, the same object lock), the listener can force only one thread to process the code block at a time, this is the most common synchronization. In addition, synchronized can synchronize memory. In practice, synchronized synchronizes the memory of all threads with the primary memory. Therefore, the execution process of geti3 () is as follows:

1. The thread obtains the object lock from the listener. (Assume that the listener is not locked. Otherwise, the thread can obtain the object lock only when the listener is unlocked)

2. Thread memory updateAllThat is, he will read the variables in the main memory so that his variables are valid. (JVM uses a "dirty" flag to optimize the process, so that only the "dirty" flag variable is updated. For details, see Java 17.9)

3. The code block is executed (in this example, set the return value to the current I3 value that has just been reset from the primary memory .)

4. Any variable changes will be written back to the primary memory. In this example, geti3 () does not change.

5. The thread releases the object lock to the listener.

Therefore, volatile can only synchronize the value of one variable between the thread memory and the main memory, while synchronized synchronizes the value of all variables between the thread memory and the main memory, and is implemented by locking and releasing the listener. Obviously, synchronized consumes more performance than volatile.

 

=============== Differences between the two ==================================

1. volatile essentially tells the JVM that the value of the current variable in the register (Working Memory) is uncertain and needs to be read from the main memory. Synchronized locks the current variable, only the current thread can access this 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 level.
3. Volatile can only realize variable modification visibility, but cannot guarantee atomicity. Synchronized can ensure variable modification visibility and atomicity.
4. Volatile will not cause thread blocking; synchronized may cause thread blocking.
5. Variables marked by volatile are not optimized by the compiler; variables marked by synchronized can be optimized by the compiler.

 

The reason for the red part is as follows:
When thread a modifies the variable, another thread B can see the modified value, and can modify the variable, instead of waiting for a to release the lock, because the volatile variable is not locked.

 

Differences and relationships between volatile and synchronized []

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.