Appendix 2 volatile and volatile

Source: Internet
Author: User

Appendix 2 volatile and volatile

Note: Before reading this chapter, you must first understand the Java memory model. For details, see Appendix 1 Java Memory Model and shared variable visibility in the previous chapter. The link is as follows:

Http://www.cnblogs.com/java-zhao/p/5124725.html

1. volatile usage

For specific usage, refer to the count attribute of the Segment internal class in Chapter 2 ConcurrentHashMap source code parsing to see how to operate on it in the case of multithreading. The specific link is as follows;

Http://www.cnblogs.com/java-zhao/p/5113317.html

Alternatively, you can refer to the underlying array of CopyOnWriteArrayList in Chapter 3 CopyOnWriteArrayList source code parsing. The specific link is as follows;

Http://www.cnblogs.com/java-zhao/p/5121944.html

 

2. Implementation Principle

  • When you perform a write operation on the volatile variable, a store barrier command is added after the write operation.
  • When performing a read operation on the volatile variable, a load barrier command is added before the read operation.

Speaker:

  • When performing a read operation on the volatile variable, you must first read the latest variable value from the primary memory to the working memory, and then read the copy of the variable stored in the working memory.
  • When a write operation is performed on the volatile variable, the changed value in the working memory is forcibly written to the master memory.

In this mode, each thread obtains the latest volatile variable value.

Note:

Volatile cannot implement atomicity:

Eg.

private volatile int count = 0;

Suppose there are two threads that add 1 to count, respectively. The expected result is count = 2 at the end, but let's look at the analysis below:

Assume that the process is as follows:

1) thread a obtains count = 0;

2) thread B obtains count = 0;

3) thread B on count + 1, and then write the data to the main memory count = 1;

4) thread a has a pair of count + 1 and then writes it to the main memory count = 1;

The result is count = 1 rather than count = 2, because volatile cannot implement atomicity after thread a obtains count. In this case, B can also operate count.

To implement atomicity, use synchronized to lock the add method, or use ReentrantLock to lock the Add code. Of course, it is better to use AtomicInteger in the above scenarios.

 

3. volatile application scenarios

  • The calculation result does not depend on the current value, such as Boolean, but number ++ does not.
  • The calculation result depends on the current value, but only the value of the variable can be modified by a single thread. For example, the count variable of Segment in ConcurrentHashMap.
  • Variables do not need to participate in the constant constraint with other state variables, such as low <up.

 

Note:

  • For more information about volatile prohibiting Command Re-sorting, see Chapter 12th "Java Memory Model and thread" in "an in-depth understanding of Java Virtual Machine (version 2"
  • Generally, synchronized is not used for solutions that can be solved with volatile.

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.