Java volatitle Multithreading

Source: Internet
Author: User
We know that the operations for setting variable values in Java are atomic except for long and double variables. That is to say, there is no need to synchronize simple read/write operations on variable values.
Before JVM 1.2, Java's memory model implementation always reads variables from the primary memory, which does not require special attention. Now, with the maturity and optimization of JVM
The use of volatile keywords becomes very important. In the current Java memory model, the thread can store variables in the local memory (such as machine registers), rather than directly in the main memory.
Read/write. This may cause a thread to modify the value of a variable in the main memory, and another thread will continue to use it to copy the variable value in the register, resulting in data inconsistency. To solve this problem, you only need
As in this program, declare the variable as volatile (unstable), which indicates that the JVM variable is unstable, each time you use it, it is read to the primary storage. Generally
In the multi-task environment, all the symbols shared by each task should be modified with volatile.

Each time a member variable modified by Volatile is accessed by a thread, the value of the member variable is forcibly re-read from the shared memory. In addition, when the member variables change, the thread is forced to write the change value back to the shared memory. In this way, two different threads always see the same value of a member variable at any time.

In the Java language specification, it is pointed out that in order to get the best speed, the thread is allowed to save the private copy of shared member variables, and the original value of shared member variables is compared only when the thread enters or leaves the synchronized code block.

In this way, when multiple threads interact with an object at the same time, you must notice that the thread needs to get the shared member variable changes in a timely manner.

The volatile keyword prompts VM: For this member variable, it cannot store its private copy, but should directly interact with the shared member variable.

Suggestion: use volatile on the member variables accessed by two or more threads. You do not need to use this variable when it is already in the synchronized code block or a constant.

Because volatile is used to block the necessary code optimization in the VM, the efficiency is relatively low. Therefore, this keyword must be used when necessary.

 

 
In the implementation of virtual machines, basic types such as int and char are a word length. The long and double characters are the same. InSomeVirtual Machine implementation, two characters longPossibleIt is operated as a single word length of two atoms.
If long and double are not modified with volatile, if multiple threads access the variable, the result is confusing due to the non-atomicity of the long operation.
For example: int, one thread writes 4, and the other writes 5. It must end with 4 or 5. But the long type may be a messy number.

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.