Analysis of the key of volatile

Source: Internet
Author: User

Synchronized
Synchronization block Everyone is more familiar with, through the Synchronized keyword to achieve, all plus synchronized and block statements, in multi-threaded access, at the same time only one thread can use synchronized modified method or block of code.

Volatile
With volatile modifiers, the thread will read the variable's most-modified value each time it uses the variable. Volatile is easily misused and used for atomic operations. The volatile reminder compiler can change the variables that are defined later, so the compiled program reads the data directly from the variable address each time it needs to store or read the variable. Without the volatile keyword, the compiler might optimize reading and storage, may temporarily use the value in the register, and if the variable was updated by another program, there would be an inconsistency.

In the case of non-atomic 64-bit operations, the Java storage model requires that both the acquired and stored operations must be atomic, and for non-volatile modified double,long types, Java splits 64 into two 32-bit operations, if read and write occur in two different threads , then there will be a worth 32 bits high and a 32 bit worth less

Volatile meaning
     volatile is always about optimization, the compiler has a technique called data flow analysis, where variables in the parser are assigned, where they are used, where they are invalidated, and analysis results can be used for constant merging Constant propagation and so on optimization, further can die code elimination. But sometimes these optimizations are not required by the program, you can use the volatile keyword to prohibit these optimizations, volatile literal meaning is variable, it has the following role:  
 1 does not cache volatile variables in registers between two operations. In multi-tasking, interrupts, and even setjmp environments, variables can be changed by other programs, and the compiler cannot know for sure, and volatile is telling the compiler this is the case.
2 does not do constant merging, constant propagation, and so on, so like the following code:  
volatile int i = 1; 
if (i > 0) ...  
If conditions are not treated as unconditional true.  
3 reads and writes to volatile variables are not optimized. If you assign a value to a variable but it doesn't work, the compiler can often omit that assignment, but the processing of memory Mapped IO cannot be optimized.  

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.