Detailed explanation of the volatile keyword and javavolatile keyword in java
I. Why should I have the volatile keyword?
It is estimated that many java beginners are confused by the volatile keyword. Although there are a lot of articles on the Internet discussing volatile, some of them talk too much about the underlying principles, but do not describe their application scenarios, which makes it difficult for beginners to understand. Some of them use analogy too much to explain, such an article is more harmful. Next, I try to analyze the role and usage of the volatile keyword, hoping to give you some inspiration. Please correct the mistakes in this article.
We know that in multi-threaded programming, when multiple threads access Shared variables, necessary synchronization is required; otherwise, errors may occur. The synchronized keyword can be used to provide such synchronization. This keyword mainly sets methods or code blocks as synchronous methods or synchronized code blocks. This synchronization can provide the following two guarantees:
1. atomicity. No matter how many threads access this method or code block, the statements in the method and code block can only be executed as a whole. That is, when a thread accesses a synchronous method, other threads can execute this method. Simply put, it is a thread and a thread, and all the statements in the synchronous method or code block must be executed.
2. Memory visibility. This refers to any changes made to shared variables by a thread in the synchronous method or code block, which is visible to other threads subsequently.
This synchronization mechanism is like a strict suit, which is very standard, but it is a bit awkward to wear and costly. Sometimes it is not very convenient to use. For example, a state variable in the program is read by multiple threads. We only require that the value read by each thread be the latest, different operations are performed according to the latest status. In this case, java provides a lightweight solution that uses volatile.
Ii. What is the role of the volatile keyword?
Volatile is "changeable, unstable; (liquid or oil) volatile; explosive; happy, brisk ". It can be seen that, literally, volatile should be used in variable variables that are easy to change and unstable. In fact, the function of this keyword is to tell the compiler that all variables declared by this keyword are variable and unstable. Therefore, do not try to use caching and other optimization mechanisms for this variable, but read the value from its memory address every time. Variables marked with volatile do not need to be locked when they are read or written. This reduces the probability of deadlock and keeps the code concise.
Note that it only means that each time you read the volatile variable, you must read it from its memory address, it does not mean that the volatile variable should be written back to the memory immediately after being modified. That is to say, volatile only provides memory visibility, but does not provide atomicity.
Iii. When to use the volatile keyword
Knowing the role of volatile, we know where it should be used. Obviously, it is better to modify the variable only by one thread and read the variable by multiple threads. That is, the memory visibility requirements are high, and the atomicity requirements are low.
For example, when writing a game, we often need to define a game state variable and modify it according to different conditions in the main thread. To improve the running efficiency of the game, we usually use multiple threads to process program data concurrently. All these threads need to access the game status variables to perform corresponding operations based on the game status. At this time, volatile is not suitable.
What is the volatile keyword in JAVA?
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.
When the volatile keyword in java can be used
In the case of multithreading, a member variable is changed by another thread.