What does the volatile keyword in Java mean? How to use it ?, Volatile keywords

Source: Internet
Author: User

What does the volatile keyword in Java mean? How to use it ?, Volatile keywords

Once a concurrent shared variable (class member variable, static member variable) being modified by the volatile keyword gives visibility (that is, the value of a variable modified by a thread is immediately visible to another thread) and orderliness (that is, the re-sorting of commands is prohibited ), in essence, a lock prefix command is added to the generated assembly code. For example, we often use the mark method to interrupt the thread, as shown below:

This code can interrupt thread 1 most of the time, but there is still a small probability that thread 1 cannot be interrupted because each thread has its own working memory, when thread 1 is running, a copy of the stop variable in the main memory will be copied to the working memory, when thread 2 changes the value of the stop variable, it cannot be written back to the primary storage before doing other things, at this time, thread 1 may not be able to immediately perceive the changes to the stop variable and cannot interrupt the logic that may cause errors. When we add the volatile modifier to the stop variable, the above problems will not exist, because volatile will force the thread to modify variable changes and immediately write them back to the primary storage, when thread 2 modifies the stop variable value, the stop cache in the working memory of thread 1 becomes invalid, and the stop value is read again from the main memory.

The guarantee of volatile orderliness has two meanings. When the program executes a read or write operation on the volatile variable, the changes in the previous operation must have been completed and the results are visible to subsequent operations, the subsequent operations are certainly not yet performed. During command optimization, statements that access volatile variables cannot be placed behind them for execution, you cannot put the statement after the volatile variable in front of it for execution.

Common scenarios of volatile include multi-threaded custom condition marking variable interrupt and Singleton mode double check.

Summary

Volatile can solve the "one-Write-multiple-read" problem, but it cannot solve the "Multiple-write" scenario. Volatile can modify arrays and collections, but is only valid for this reference and invalid for its elements. Vollatile can ensure its visibility, but it cannot guarantee atomicity. For example, double and long operations in multiple threads are not atomic operations. read and write the first 32 bits, and then read and write the last 32 bits, in this scenario, atomicity can be ensured.

I have a public account that will often share some Java-related things. If you like it, you can search for "Java headers" or "javatuanzhang.  

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.