Use of voltatile keywords in Java

Source: Internet
Author: User

1. First understand the concept of main memory and thread working memory in Java.

If there is a static variable, the value is stored in main memory. If multiple threads access this variable, each thread copies the value of the variable to its own working memory, followed by the operation of the copy in its own working memory, and then back to the main memory

Obviously, the above operation is non-atomic, and there is a classic multithreading problem:

    Variable i=6, threads A and B both add 1 to I, expect I to be 8, but: Thread A    accesses I, thread    a i++ operation    thread B accesses I, thread    b makes i++ operation thread    a update i,i to 7    thread B update I , I is 7

2. The volatile keyword tells the JVM that I am accessing this variable, does not need to create a copy of the working memory, I want to directly manipulate the main memory variables, it involves the visibility of multi-threading:

For visibility, Java provides the volatile keyword to guarantee visibility.

When a shared variable is modified by volatile, it guarantees that the modified value is immediately updated to main memory, and when other threads need to read it, it will read the new value in RAM.

The common shared variable does not guarantee visibility, because when a common shared variable is modified, it is indeterminate when it is written to main memory, and when other threads go to read it may be the original old value at this time and therefore cannot guarantee visibility.

3. However, volatile still does not guarantee atomicity, and what is said in 1 will still occur, so volatile can guarantee visibility and cannot guarantee atomicity

4. What is the difference between atomicity and visibility?

Atomicity is the execution of an operation that cannot be interrupted.

Visibility is the result of the execution of an operation immediately perceived by other operations, which, unlike atomicity, cannot guarantee that this operation will not be interrupted, but that the results of this operation can be perceived by other operations

Use of voltatile keywords in Java

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.