Java multithreaded volatile keyword

Source: Internet
Author: User

The JVM is divided into JVM client mode, JVM server mode. The difference is not big, but the serve startup mode starts slowly, but once it is running, the efficiency in hash and method call is dozens of times times higher than the client mode.

When the thread reads the data, it first reads the data from the main memory, saves it in the local working memory, modifies the data, modifies it, writes to the local working memory, and then writes the main memory from the working memory. The example diagram is as follows:

If the JVM is started in server mode, for the sake of thread efficiency, the thread will save the data in working memory after the first time it is read, and then read the data from the working memory. However, if the main memory modifies the data, it causes the data in the working memory to be inconsistent with the main memory data. Causes a logic error.

The volatile keyword can only modify variables. When a variable is modified with volatile, each read variable is forced to read from the main memory, which guarantees the consistency of the variables. Data is guaranteed to be visible, but it does not guarantee the atomicity of the data.

Java multithreaded volatile keyword

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.