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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the application of Java's memory model". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First of all, the definition is given. The Java memory model (Java Memory Model, JMM) is a mechanism and specification that conforms to the memory model specification, shields the access differences of various hardware and operating systems, and ensures the consistent effect of Java program access to memory on various platforms.
Before we understand JMM, we need to understand how CPU and memory interact.
Interaction between CPU and cache and memory (main memory)
It can be seen from the figure that in a multi-CPU system, each CPU has its own cache, which is generally divided into L1, L2 and L3 caches, because the existence of these caches not only provides data access performance, but also reduces the pressure of data transmission on the data bus, but there is only one main memory.
When CPU wants to read a data, it first looks it up from the first-level cache, and then from the second-level cache if it is not found. If it still hasn't, it looks for it from the third-level cache or memory. Each CPU has and only has its own cache.
But the question is, what happens if two CPU operate on the same memory address at the same time? In other words, how to ensure cache consistency in multi-threaded scenarios when multiple processors operate in the same memory area? How to ensure data consistency at run time? That is the memory barrier (Memory Barrier).
Memory barrier
The cache in CPU improves the performance of data access, avoids asking for memory every time, but cannot exchange information with memory in real time. Different threads executing in different CPU may have different cache values for the same variable, thus there is a memory barrier. There are two kinds of memory barriers in the hardware layer: Load Barrier and Store Barrier, namely read barrier and write barrier.
There are two main functions of the memory barrier:
Prevent instruction reordering on both sides of the barrier
Force the dirty data in the write buffer / cache to be written back to the main memory to invalidate the corresponding data in the cache.
The reason why there are so many computer memory models is that the setting of the Java memory model conforms to the computer specification.
In fact, JMM is a specification of JVM that defines the memory model of JVM.
It shields the access differences of various hardware and operating systems, and does not directly access hardware memory like C, so it is much more secure.
Its main purpose is to solve the problems caused by the inconsistency of local memory data, the reordering of code instructions by the compiler and the out-of-order execution of the code by the processor when multi-threads communicate through shared memory. Atomicity, visibility, and orderliness can be guaranteed in concurrent programming scenarios.
Application of Java memory Model
Several keywords in Java: volatile, final, synchronized, can help programmers describe the concurrency requirements in their code to the compiler. Their behavior is defined in the Java memory model to ensure that correctly synchronized Java code executes correctly on all processor architectures.
Volatile
In Java, the volatile keyword solves the above problem, and Java masks these differences and generates memory barrier instructions through JVM.
When we declare a variable as volatile decorated, the variable has thread visibility, and volatile adds a memory barrier before and after the read and write operation. Each read behavior of the volatile field can see the last time another thread writes to the field, so you can avoid getting stale data in the cache. They must be guaranteed to be flushed to main memory after being written so that they can be immediately visible to other threads.
Final
If a class contains a final field and is initialized in the constructor, the final field is visible to other threads after the correct construction of an object.
Note that the correct construction of the object here means that references to the object are not allowed during the construction of the object, otherwise, other threads may access the object before the object is constructed, causing other problems.
Synchronized
For a monitor object decorated by synchronized, it can only be held by one thread, which means that once a thread enters the synchronous code block, other threads cannot enter until the first thread exits the code block.
When a thread exits the synchronization block, the thread releases the monitor object, which flushes the CPU cached data (locally cached data) into main memory so that the thread's behavior can be seen by other threads. When other threads enter the code block, they need to get the monitor object, which is used to invalidate the CPU cache so that the variable is reloaded from main memory, and then you can see the previous thread's modifications to the variable.
But from a caching point of view, this problem only affects multiprocessor machines, which is fine for a single core, but it also has a semantics that forbids reordering of instructions, and for the compiler, the code in the synchronous block does not move outside of getting and releasing monitor.
Summary
JMM is a specification of JVM that defines the memory model of JVM. Its main purpose is to solve the problems caused by the inconsistency of local memory data, the reordering of code instructions by the compiler and the out-of-order execution of the code by the processor when multi-threads communicate through shared memory. Atomicity, visibility, and orderliness can be guaranteed in concurrent programming scenarios.
In Java, the three keywords volatile, final, and synchronized are concrete implementations of the and memory model.
This is the end of the content of "the Application of Java's memory Model". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.