Detailed description of the volatile keyword in Java

Source: Internet
Author: User


First, why should have the volatile keyword

It is estimated that many Java beginners are confused by the keyword volatile. Although there are many articles on the internet to discuss volatile, but they have too much to tell the underlying principle, but not the application of the scenario, so that beginners to see or confused, some too use analogy to explain, resulting in a certain error, such articles more harmful. Below, the younger brother tries to analyze the function and usage of the volatile keyword, hoping to bring some inspiration to everyone. The error in the text, please the great god correct you.

We know that in multithreaded programming, multiple threads have to make the necessary synchronizations when accessing shared variables, or they are likely to produce errors. The Synchronized keyword can be used to provide this synchronization. This keyword mainly sets the method or block of code as either a synchronous method or a synchronized code block. This synchronization can provide the following two guarantees:

1. Atomic nature. Regardless of how many threads are accessed by this method or block of code, the methods and statements in the code block can only be executed as a whole, that is, when a thread accesses a synchronous method, other threads can execute the method only when the method finishes executing. Simply put, it is a thread, and all the statements in the synchronization method or code block must be executed.

2, memory visibility. This refers to any changes that a thread makes to a shared variable in a synchronous method or block of code that is visible to subsequent threads.

This synchronization mechanism is like a set of thorough suits, very standard, but it is a little awkward to wear, the cost is big. Sometimes it is not very convenient to use, for example, there is a state variable in the program, is read by multiple threads, we only require each thread to read the value is the latest value, and according to the latest state to take different actions. In this case, Java provides us with a lightweight solution that uses volatile.

Ii. What is the role of the volatile keyword

The English meaning of volatility is "volatile, unstable, volatile" (liquid or oil), explosive, merry, brisk. It can be seen that, as a single literal understanding, volatile should be used on variable, unstable variables. In fact, indeed, the function of this keyword is to tell the compiler that variables that are generated by the keyword are variable and volatile. So instead of trying to use an optimization mechanism like caching for that variable, you should read the value from its memory address every time. Variables that use volatile tags do not need to use locks when reading or writing, which reduces the probability of deadlocks and keeps the code concise.

Note that this is only said to be read from its memory address every time a volatile variable is read, and it does not say that the value of the volatile variable is immediately written back to memory each time it is modified. This means that volatile provides only memory visibility, not atomicity.

Third, when to use the volatile keyword

Knowing the role of volatile, we also know where it should be used. Obviously, it's best to have one thread that modifies a variable, where multiple threads read the variable. This is the requirement for high memory visibility and low atomicity.

For example, when we write a game, we often need to define a game's state variable and modify it in the main thread according to different conditions. In order to improve the efficiency of the game, we usually use multiple threads to process the program data, these threads have to access the game's state variables in order to follow the game state of the corresponding operation. This time with the volatile will be more appropriate.


Detailed description of the volatile keyword 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.