In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of the java memory model in jvm, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
1. What is the difference between java memory model and java memory structure? 1. Java memory structure
I remember that when I was studying Android several years ago, the java memory model I saw was often indistinguishable from the memory structure of java, so this section is aimed at rookies or people who don't understand its concept.
As we all know, our java code can not be run directly, it has to go through a series of steps. Look at the following picture:
Our java file must first be programmed into a class file, and then loaded into jvm through the class loader for execution. The jvm (the part framed by the red dotted line) is the java runtime data area, which means that when the java code runs, the data is stored in different memory spaces. This is what jvm refers to. Of course, the above runtime datazone jvm is the jdk1.7 version. In other words, different versions of jdk, this jvm looks different. We can take out the memory structure of java7:
We can see that it is divided into five parts, of which the java heap area and the method area are still an area shared by all threads. Why should all threads share? Because suppose a piece of data, each thread keeps a copy, then one of the threads is naughty and changes the data. Other threads find that their data has not changed, which is a problem. So it was designed to be shared by all threads, and the java memory model came out.
2. Java memory model
The java memory model is also called JMM, but this model is not real like the java memory structure. The java memory model is an abstract concept. It means that a part of the memory area is designed to be shared by all threads, and one thread makes changes to the data, and other threads can know immediately. This method of design is called the memory model. We can take a look ahead of what the java memory model looks like.
This is the java memory model, where multiple threads share the same data. Now I don't know if you understand the difference between the java memory model and the java memory structure, let's sum it up like this:
(1) the java memory structure is to solve the problem of how to store the data in java.
(2) the java memory model is to solve the problem that multiple threads share data in java.
OK, this is basically the end of the difference between the two. Let's see why there is a memory model.
Second, why should there be a memory model
In-depth understanding of the java virtual machine is analyzed from the development of hardware. Therefore, I will also analyze it from this perspective.
Stage one
In the first stage of computer development, programs are run in CPU and data are stored in main memory. With the development of technology, the speed of CPU is getting higher and higher, but the speed of main memory is not improved much. It's like this:
Stage two
In order to solve the above problems, there is a cache, which stores some main memory data often used by CPU, and the cache speed is about the same as CPU. When CPU looks for data, it first looks it up from the cache, and if not, it looks up from the main memory again. When writing data, write the cached data first, and then update it to main memory. Such a mechanism makes the speed much faster.
Stage three
Technology continues to develop, on the basis of the above cache, there is a first-level, two-level, three-level cache, lookup is also layer-by-layer, the first-level cache does not go to the second layer, and so on. At this time, CPU has also been fast-reading development, from a core to a multi-core CPU (a CPU has become a multi-part).
At this time, before, you could only run one thread at the same time, but now you can run many threads. And as we can see from above, each core has a corresponding cache area, but which one is the main memory? Since it can run multiple threads at the same time, then the speed must be on the bar drop, do not run do not know, a run scared, immediately appeared a lot of problems.
Problem 1: cache consistency
In other words, each core has its own cache, but these caches hold different data. One picture will make it clear:
Problem 2: processor optimization and instruction rearrangement
What the problem means is that since CPU has so many cores, it must want to make full use of its resources, so we split the program we wrote and disordered some of the code, which is processor optimization. Moreover, as soon as the java virtual machine saw that this operation of CPU was really strong, it imitated it and created a just-in-time compiler (JIT), which would also rearrange instructions. Obviously, when our code order is disrupted and instructions are rearranged, it may not be carried out as we wish.
The above problems seem to be analyzed from the perspective of hardware, and the book "in-depth understanding of the java Virtual Machine" leads to the problem of software, that is to say, what problems will the above problems bring if they are converted to the software level?
Question 3: software problems
(1) atomicity
First of all, the cache consistency problem will cause atomicity problems in the program. What does atomicity problem mean? You have to understand the atom first. In living things, atoms are called inseparable substances. In software, atoms are called indivisible program operations. The problem of atomicity must have broken this rule, that is to say, it was split again in this operation.
In the issue of cache consistency, the data of an in the two CPU kernels is inconsistent, that is, the values of a read by the two CPU cores are not the same. Then the operation of changing an is certainly not atomic, and during the change of A, both CPU cores change at the same time.
(2) visibility issues
As mentioned above when introducing atomicity, when two threads (CPU kernel) access the same variable, thread 2 modifies the value of the variable, but thread 1 does not see the change, so it is still reading the old data.
(3) order
That is, the program is not executed in the specified order.
Visibility and orderliness problems are caused by processor optimization and execution rearrangement.
Stage four
For so many problems, the java virtual machine proposes a java memory model. Effectively solve the above three problems:
3. Java memory model 1. Explanation
To compare with the beginning, we give a diagram of his memory model:
From this diagram, we analyze how the java memory model solves the above three problems.
Rule 1: all data is in main memory.
Rule 2: each thread keeps a copy of the shared variable. All operations on the variable by the thread must be done in this copy memory and cannot be read or written directly to the main memory.
Rule 3: there is no direct access to variables in each other's working memory between different threads, and the transfer of variables between threads requires data synchronization between their own working memory and main memory.
Look at the text is a bit messy, let's give an example, I do not think this example is appropriate, but combined with the above concept, I believe you will understand. In ancient times, there were often droughts, and the court went to the disaster relief, so they took a big iron pot to cook porridge. This big iron pot is the main memory, and the porridge inside is the data. And each refugee represents a different thread.
(1) all the refugees have a bowl containing a bowl of porridge. It is as if each thread has a local memory and a copy of the main memory.
(2) the refugees can only eat porridge in their own bowls, not directly into the pot to eat porridge, just as threads can only operate data in their own local memory and cannot read and write data directly to main memory.
(3) what if one of the refugees wants to share the porridge with his partners? The refugee first poured the porridge into the pot, and then the companion went to the pot to fill it. Just as threads cannot access each other's memory directly, the data between them is transferred through the main memory.
I hope you can understand this example. Now that this model is out, there is another question that remains unsolved, and that is, what does java provide to implement these three rules?
Because there are too many mechanisms provided, we can simply cite a few, such as the synchronized keyword ensures atomicity and the volatile keyword ensures visibility. The synchronized keyword and volatile keyword guarantee orderliness. Of course, there are a lot of Lock mechanisms, concurrent distribution packages and so on are put forward to solve these three problems.
2. Happens-before principle
One of the most important rules is called the happens-before principle, which is proposed to address visibility. What do you mean?
If the execution result of one operation needs to be visible to another operation, then there must be a happens-before relationship between the two operations. For example:
(1) Program order rules: each operation in a thread occurs before the latter operation, which is called happens-before.
(2) Lock rules: for the locking mechanism, it must be locked before it can be unlocked, which is also happens-before.
(3) volatile domain rule: a write to a volatile domain must occur before the read operation.
The above is from a procedural point of view, to take the simplest example, you have to cook before you can eat.
Thank you for reading this article carefully. I hope the article "sample Analysis of java memory Model in jvm" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.