The difference between volatile and synchronized

Source: Internet
Author: User
Tags visibility volatile

When studying concurrent programming, it involves the volatile,volatile and the JVM memory model, which is the knowledge of previous learning, and now some forget, as review and learn again. Because the research is shallow, fear of expression is not clear offended the principle, is not here to say volatile principle and JVM memory model, a better explanation volatile key from the blog can be used as a reference: Java concurrent Programming: volatile keyword parsing

Reference to the book is Java session famous: Java Programming ideas


Because I am from the Synchronized keyword infiltration high concurrency, originally want to according to their own understanding to do under the record, but found a good summary of predecessors, so I reproduced here, about volatile and synchronized difference:

Turn from: http://www.cnblogs.com/tf-Y/p/5266710.html

Thank bloggers for sharing.


1, the lock provides two main characteristics: mutual exclusion (mutual exclusion) and visibility (visibility).

Mutual exclusion allows only one thread to hold a particular lock at a time, so you can use this attribute to implement a coordinated access protocol to shared data so that only one thread at a time can use the shared data.

Visibility is a little more complicated, it must ensure that the changes made to the shared data before the lock is released are visible to another thread that subsequently obtains the lock-if there is no such visibility guaranteed by the synchronization mechanism, the shared variable that the thread sees may be the value before the modification or inconsistent, which can cause many serious problems. (race condition)


2, in Java, in order to ensure that the data is multi-threaded reading and writing to ensure consistency of data, you can use two ways:

Sync: such as using the Synchronized keyword, or using a lock object

Use the volatile keyword: summarize the volatile in a single sentence, which allows the variable to be known as quickly as possible when the value changes.


3, volatile detailed

First of all, we have to realize that there is such a phenomenon, the compiler in order to speed up the operation of the program, the write operations on some variables will be on the register or CPU cache, the last to write memory .
In this process, the new value of the variable is not visible to other threads.

when a variable of the volatile token is modified, the modified variable stored in the other cache is purged and then re-read. Where to read from I am not clear, in general should be in the modified cache A is modified to the new value, the other cache is then notified that the variable is purged, and when a thread in other cache B reads the variable, it sends a message to the bus, where the cache a that stores the new value gets the message and the new value is passed to B. Finally, the new value is written to memory. This is the step when the variable needs to be updated, and the volatile is the variable that it modifies, and the steps are refreshed each time it is updated.


4, volatile and synchronized

1 volatile is essentially telling the JVM that the current variable's value in the register is indeterminate, needs to be read from main memory, synchronized is locking the current variable, and other threads are blocked if the line can access the variable.
2 volatile can only be used at variable levels, synchronized can be used in variables, methods.
3) The volatile can only realize the modification visibility of the variable, while synchronized can guarantee the modification of the variable visibility and atomicity.

 The Java programming idea says that when you define a long or double variable, if you use the volatile keyword, you get the atomicity of the (simple assignment and return operation)
4 volatile does not cause the thread to block, and synchronized may cause the thread to block.


5, when the value of a domain depends on its previous value, volatile can not work, such as n=n+1,n++. If the value of a field is limited by the value of another domain, then volatile cannot work, such as the lower and upper bounds of the range class, and must follow the lower<=upper limit.


6. The only security situation that uses volatile instead of synchronized is that there is only one mutable domain in the class


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.