Description of volatile variables in Java

Source: Internet
Author: User

volatile variables have two characteristics :

    • Ensure that this variable is visible to all threads, note that the operations in Java are not atomic operations, resulting in inconsistent problems with volatile variables in concurrency.

      Use rule requirements

      A. The result of the operation does not depend on the current value of the variable, or can ensure that only a single thread modifies the value of the variable.

      B. Variables do not need to participate in invariant constraints with other state variables.

    • Disable command reordering optimization


Other Notes:

The following eight operations (atomicity) are defined in the Java memory model:

1.lock (Locked): A variable that acts on the main memory, which identifies a variable as a thread-exclusive state.

2.unlock (Unlocked): A variable that acts on the main memory, releasing a variable that is in a locked state, and the released variable can be locked by another thread.

3.read (Read): A variable that acts on the main memory, transferring the value of a variable from main memory to the working memory of the thread for subsequent load actions to use.

4.load (load): A variable that acts on working memory, which places the value of a read operation from memory into a variable copy of the working memory.

5.use (used): A variable acting on a working memory that passes the value of a variable in the working memory to the execution engine, which is performed whenever the virtual opportunity is to a bytecode instruction that needs to use the value of the variable.

6.assign (Assignment): A variable acting on a working memory that assigns a value to the working memory from the execution engine, and performs this operation whenever the virtual opportunity is given to a directive instruction that assigns a value to a variable.

7.store (storage): A variable acting on a working memory that passes the value of a variable in the working memory to main memory for subsequent write operations to use.

8.write (Write): A variable that acts on working memory, which puts the value of a store operation's variable from the working memory into a variable in the main memory.


Volatile variables cause variable operations to be associated with load and use actions associated with assign and store, write actions, and must appear consecutively, while Action Association rules ensure that variables are not optimized for command reordering.


Non-atomic protocol for long and double: allows virtual machine implementations to choose the atomicity of the four operations of load, store, read, and write that do not guarantee 64-bit data types (64-bit read and write operations are divided into two 32-bit operations). In real-world development, commercial virtual machines for each platform are treated as atomic operations, so they do not need to be specifically declared volatile.


When measuring concurrency security issues, do not be disturbed by chronological order, and everything must be based on the principle of antecedent occurrence.



Description of volatile variables 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.