In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the function of Volatile". In daily operation, I believe that many people have doubts about the role of Volatile. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the role of Volatile?" Next, please follow the editor to study!
1. Related understanding 1.1. The memory Model of Modern computer
In fact, the speed of cpu and memory in early computers is about the same, but in modern computers, the instruction speed of cpu is much faster than the access speed of memory, because there is a gap of several orders of magnitude between the computing speed of the computer's storage device and the processor. Therefore, modern computer systems have to add a cache (Cach) whose read and write speed is as close as possible to the processor speed as a buffer between memory and processor.
Copy the data needed for the operation into the cache, so that the operation can be carried out quickly, and then synchronized back to memory from the cache when the operation is over, so that the processor does not have to wait for slow memory reads and writes.
Cache-based storage interaction solves the speed contradiction between processor and memory, but it also brings higher complexity to computer systems, because it introduces a new problem: cache consistency (CacheCoherence).
In multiprocessor systems, each processor has its own cache, and they share the same main memory (MainMemory).
1.2.JMM (JavaMemoryModel)
JMM:Java memory model is a memory model defined in java virtual machine specification. Java memory model is standardized, shielding the differences between different underlying computers. This paper describes the access rules of various variables (thread-shared variables) in Java programs, as well as the low-level details such as storing variables in JVM and reading variables from memory.
1.2.1JMM has the following regulations
All shared variables are stored in main memory, and the variables here refer to instance variables and class variables, which do not contain local variables, because local variables are thread private, so there is no competition problem.
Each thread also has its own working memory, and the thread's work content retains a working copy of the variables used by the thread.
All operations (read and fetch) on variables by a thread must be done in working memory, rather than directly reading and writing variables in main memory.
The variables in each other's working memory can not be accessed directly between different threads, and the value of variables between threads needs to be transferred through the main memory.
The relationship between local memory and main memory:
It is precisely because of this mechanism that the visibility problem exists.
2. Visibility solution locking / * * @ Author pre-court cloud fall * @ Date 10:43 on 2020-10-1 * @ Description * / public class Test {public static void main (String [] args) {Tqyl a = new Tqyl (); a.start (); for (; ) {synchronized (a) {if (a.isFlag ()) {System.out.println ("pretrial clouds");}} 2.1. Why does locking solve the visibility problem?
Because before and after a thread enters the synchronized code block, the thread will acquire the lock, empty the working memory, copy the latest value of the shared variable from the main memory to the working memory to become a copy, execute the code, refresh the value of the modified copy back to the main memory, and the thread releases the lock.
Threads that cannot acquire the lock block waiting, so the value of the variable must always be up-to-date.
2.2.Volatile modifies the shared variable / * @ Author pre-court cloud settlement * @ Date 10:44 on 2020-10-1 * @ Description * / public class Tqyl extends Thread {private volatile boolean flag = false; public boolean isFlag () {return flag;} @ Override public void run () {try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace () } flag=true; System.out.println ("flag=" + flag);}} what did 2.3.Volatile do?
When each thread manipulates the data, it will read the data from the main memory to its own working memory. If it manipulates the data and writes it back, the variable copies of other read threads will be invalidated. If you need to read the data into the operation, you have to read it in the main memory again.
Volatile ensures the visibility of different threads on shared variable operations, that is, one thread modifies volatile-decorated variables, and when the changes are written back to main memory, the other thread immediately sees the latest value.
We have said before that when the computing tasks of multiple processors involve the same main memory area, it may lead to inconsistent cache data. An example is given to illustrate the sharing of variables among multiple CPU.
If this happens, whose cached data will prevail when synchronization goes back to main memory?
In order to solve the problem of consistency, each processor needs to follow some protocols when accessing the cache and operate according to protocols when reading and writing, such as MSI, MESI (IllinoisProtocol), MOSI, Synapse, Firefly, DragonProtocol and so on.
3. MESI (Cache consistency Protocol)
When CPU writes data, if it is found that the operating variable is a shared variable, that is, a copy of the variable exists in other CPU, it will send a message to other CPU to set the cache row of the variable to an invalid state, so when other CPU needs to read the variable, it will re-read it from memory if it finds that the cache line caching the variable in its own cache is invalid.
3.1. As for how to find out whether the data is invalid? Sniffing (eavesdropping on packets flowing over a network)
Each processor checks whether the value of its cache has expired by sniffing the data propagated on the bus. when the processor finds that the memory address of its cache line has been modified, it will set the cache line of the current processor to an invalid state. when the processor modifies this data, it will re-read the data from the system memory to the processor cache.
4. Bus Storm
Due to the MESI cache consistency protocol of Volatile, it is necessary to constantly sniff from the main memory and loop the cas constantly. Invalid interaction will lead to the peak bus bandwidth.
So don't use Volatile a lot, as to when to use Volatile and when to use locks, it's different according to the scene.
CAS (Compare-and-Swap), that is, compare and replace, is a technique often used to implement concurrent algorithms, and many classes in Java concurrent packages use CAS technology.
5. Prohibit instruction reordering 5.1. What is reordering?
To improve performance, compilers and processors often reorder instructions in a given order of code execution.
5.2. What are the types of reordering? What will be reordered from the source code to the final execution?
A good memory model actually relaxes the shackles on processor and compiler rules, that is, both software and hardware technologies strive for the same goal: to improve execution efficiency as much as possible without changing the results of program execution.
JMM minimizes the constraints on the underlying layer so that it can give full play to its own advantages.
Therefore, when executing a program, compilers and processors often reorder instructions in order to improve performance.
General reordering can be divided into the following three categories:
Compiler-optimized reordering. The compiler can rearrange the execution order of statements without changing the semantics of a single-threaded program.
Instruction-level parallel reordering. Modern processors use instruction-level parallel technology to execute multiple instructions overlapped. If there is no data dependency, the processor can change the execution order of the statements corresponding to the machine instructions
Reordering of memory systems. Because the processor uses caching and read / write buffers, it appears that load and store operations may be performed out of order.
There is another concept to mention here, as-if-serial.
5.3.as-if-serial
No matter how much you reorder, the result of execution in a single thread cannot be changed.
Compilers, runtime, and processors must all follow as-if-serial semantics.
So how does Volatile guarantee that it won't be reordered?
5.4. Memory barrier
The java compiler inserts memory barrier instructions in place when generating a series of instructions to prevent reordering of certain types of processors.
In order to implement the memory semantics of volatile, JMM restricts certain types of compiler and processor reordering, and JMM establishes a table of volatile reordering rules for the compiler:
It should be noted that volatile writes insert memory barriers in the front and rear respectively, and volatile read operations insert two memory barriers in the back.
Write
Read
In order to improve the processing speed, JVM will compile and optimize the code, that is, instruction reordering optimization. Instruction reordering under concurrent programming will bring some security risks, such as the invisibility of multiple thread operations caused by instruction reordering.
If programmers are asked to understand these underlying implementations and specific rules, the burden on programmers will be too heavy, seriously affecting the efficiency of concurrent programming.
Starting from JDK5, the concept of happens-before is put forward, which is used to explain the memory accessibility between operations.
5.5.happens-before
If the result of one operation needs to be visible to another operation, then there must be a happens-before relationship between the two operations.
Volatile domain rule: write to a volatile domain, happens-before subsequent reads to that volatile domain by any thread.
If my flag becomes false now, then the latter operation must know that I have changed.
We need to know that there is no way to guarantee atomicity in Volatile. Atomicity must be guaranteed. Other methods can be used.
Atomicity cannot be guaranteed.
It is an operation that is either a complete success or a complete failure.
Assuming that there are now N threads accumulating the same variable, there is no way to guarantee that the result is correct, because the process of reading and writing is not atomic.
To solve the problem, either use atomic classes, such as AtomicInteger, or add locks (remember to pay attention to the underlying layer of Atomic)
Apply / * @ Author pre-court cloud settlement * @ Date 11:53 on 2020-10-1 * @ Description * / public class Singleton {/ / visibility and instruction reordering ensure private volatile static Singleton instance = null / / Private construction public Singleton () {} public static Singleton getInstance () {/ / first check lock if (instance==null) {/ / synchronous lock code block synchronized (Singleton.class) {/ / second check lock if (instance==null) { / / Note: non-atomic operation instance = new Singleton () } return instance;}}
Why double check? What happens if you don't use Volatile?
The benefits of disabling instruction reordering.
Object actually goes through the following steps to create an object:
Allocate memory space
Call the constructor to initialize the instance
Return the address to the reference
Instruction reordering may occur, so it is possible that the constructor completes the assignment before the object initialization is completed, opens up a storage area in memory and directly returns the memory reference, and the object is not really initialized at this time.
But other threads judge instancepointer null and use it directly. In fact, this object is a semi-finished product, so there is a null pointer exception.
How is visibility guaranteed?
Because of visibility, thread An initializes the object in its own memory, before it has time to write back to main memory, and thread B does so, so multiple objects are created, not really singletons.
6. The difference between Volatile and Synchronized
Volatile can only decorate instance variables and class variables, while synchronized can decorate methods and code blocks.
Volatile guarantees data visibility, but does not guarantee atomicity (multithreaded write operations, not thread safety); synchronized is an exclusive (mutually exclusive) mechanism. Volatile is used to prohibit instruction reordering: it can solve the problem of out-of-order execution of initialization code for single double-check objects.
Volatile can be seen as a lightweight version of synchronized,volatile that does not guarantee atomicity, but if you assign multiple threads to a shared variable without other operations, you can use volatile instead of synchronized, because the assignment itself is atomic, and volatile ensures visibility, so thread safety can be guaranteed.
7. Summary
The volatile modifier applies to the following scenarios:
A property is shared by multiple threads, one of which modifies the property, and other threads can immediately get the modified value, such as booleanflag; or as a trigger, for lightweight synchronization.
The read and write operations of the volatile attribute are unlocked, and it is not a substitute for synchronized because it does not provide atomicity and mutex. Because there is no lock, it does not take time to acquire and release the lock, so it is low-cost.
Volatile can only work on attributes, and we decorate the property with volatile so that compilers does not reorder the property.
Volatile provides visibility, any changes made by one thread will be immediately visible to other threads, and the volatile property will not be cached by the thread and will always be read from main memory.
Volatile provides happens-before guarantees for writing to the volatile variable v and subsequent read and write operations to v by all other threads.
Volatile can make the assignment of long and double atomic.
Volatile can achieve visibility and disable instruction reordering in a single case double check, thus ensuring security.
At this point, the study of "what is the role of Volatile" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.