Introduction to Java Concurrency programming-volatile visibility

Source: Internet
Author: User
Tags visibility volatile

Objective

To learn good Java Multi-threading, it is important to the volatile keyword mechanism is cooked in the chest. Recently the blogger read a lot about the volatile related blog, have a little preliminary understanding and understanding, the following through their own words of the narrative finishing.

What's the use?

Volatile mainly provides two functions for the modified variables.

    • Visibility of
    • Prevent command reordering

<br> This blog focuses on volatile visibility, and later publishes post on order reordering.

What is visibility?

A picture wins thousands of words

The Java Memory model (JMM) has been shown in detail, briefly summarizing

    1. Each thread has a working memory of its own (it can be understood that each chef has a wok that belongs to him)
    2. All thread share one main memory (all chefs in the restaurant share the same refrigerator)
    3. The data is fetched in the main memory before each thread operates the data (the chef will go to the refrigerator to get the ingredients before cooking)
    • Thread: Chef
    • Working Memory: Wok
    • Store&load: Put the cooked food, take the ingredients
    • Main Memory: Refrigerator

Readers can think about the following scenarios:<br>
The restaurant came a customer ordered a portion of the pork, at this time there are two chefs (assuming that the chef does not communicate with each other), as a result of non-communication, so two chefs open the refrigerator to remove the ingredients to start cooking.
Finally, two servings of pork, the customer only one copy. Why does this result?

Due to the lack of visibility between chefs.

Putting this scenario in Java is:<br>
Thread A takes a variable from the main memory into the working memory and does not put it back into the main memory in time, so thread B takes the variable that has expired and takes the variable before the thread a operation.

How do I have visibility?

Let's start with the atomic operation between the 8 working memory and main memory defined in the Java memory model

    • Lock: A variable that acts on the main memory and identifies a variable as a thread-exclusive state.
    • Unlock (Unlocked): A variable acting on the main memory , releasing a variable that is locked, releasing the variable to be locked by another thread.
    • 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.
    • Load: A variable that acts on working memory , which places the value of a read operation from the main memory into a variable copy of the working memory.
    • Use: A variable that acts on a work -stored type, which 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.
    • Assign (Assignment): A variable in the working memory that assigns a value from the execution engine to the working memory, which is performed whenever the virtual opportunity is to a byte-code instruction that assigns a value to a variable.
    • Store: A variable acting on a working memory that transfers the value of a variable in the working memory to main memory for subsequent write operations to use
    • Write: A variable that acts on the main memory and puts the store operation's value from the working memory into a variable in the main memory.
The case of reading an ordinary variable assigned to a value


When thread 1 initiates a read operation to the main memory object to the write operating set process, thread 2 May at any time initiate a second set of operations on the main memory object

    • What harm does it have?

Assume that there is one in main memory

int a=0;

Thread 1 and Thread 2 execute one at a time, ideally the value of end a is 2.

a++;

Thread 1 After performing the assign operation, the true value of variable A has changed from 0 to 1, but this process occurs in the working memory of the other threads are not visible, if the thread 2 operation on variable A, read the value is still 0, because there is no visibility, the operation of thread 2 is simply repeating the operation of thread 1, Let a again change from 0 to 1. did not achieve the desired a=2.

Reading an assignment a volatile variable


Volatile variables work more closely on objects:

    • Use cannot be read&load before use
    • Assign must follow Store&write

Which means that read-load-use and assign-store-write became two indivisible atomic operations.

Although there is still a vacuum between use and assign, it is possible that the variables will be read by other threads, but the values of the main memory variables and any working memory variables are equal at any point in time. This feature causes the volatile variable to be unsuitable for operations that depend on the current value, such as self-increment.
So depending on the visibility of the characteristics of volatile can be used in which areas?
"Java Virtual machine" mentions:

The result of the operation does not depend on the current value of the variable (that is, the result is not dependent on the intermediate result), or to ensure that only a single thread modifies the value of the variable

Typically volatile is used as a Boolean value to hold a state .

Some references from

  • The difference between volatile variables and ordinary variables
  • << deep understanding of Java Virtual Machine advanced features and best practices >>

Introduction to Java Concurrency programming-volatile visibility

Related Article

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.