In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge about "what is the difference between java memory model and java memory structure". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
What is the difference between Java memory model and Java memory structure? Java memory structure
I remember that when I studied Android a few years ago, I looked at the java memory model, which was often unclear from the java memory structure. Therefore, this section is aimed at people who are white or who do not understand its concept.
We all know that our java code is actually not directly run, it has to go through a series of steps. See below:
Our java files are first programmed into class files and then loaded into jvm via class loaders for execution. This jvm (the part framed by the red dotted line) is the java runtime data area, which means that when java code is running, these data are stored in different memory spaces. JVM refers to this. Of course, the runtime data area jvm above is jdk version 1.7. That is to say, different versions of jdk, this jvm looks different. We can take out the memory structure of Java 7:
We can see that there are five sections, of which the java heap and method sections are shared by all threads. Why share all threads? Because suppose a piece of data, each thread keeps a copy, and one of the threads is naughty and changes the data. The other threads find that their data has not changed, and this raises a problem. So the design was shared by all threads, and the java memory model came out.
Java memory model
Java memory model is also called JMM, but this model is not real like Java memory structure. Java memory model is an abstract concept. This means that a portion of the memory area is designed to be shared by all threads, so that one thread changes the data and the other threads know it immediately. This method of design is called the memory model. Let's take a look ahead at 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 Java memory model and Java memory structure, we can summarize it like this:
(1) Java memory structure is to solve the problem of how to store data in Java.
(2) Java memory model is to solve the problem of multiple threads sharing data in Java.
OK, basically, that's the difference between the two. Let's see why there is a memory model
Why do we need a memory model?
Java virtual machine is analyzed from the development of hardware. I will also analyze it from this perspective.
stage one
In the first stages of computer development, programs were run in CPU and data was stored in main memory. With the development of technology, CPU speed is getting higher and higher, but the speed of main memory has not improved much. It's like this:
stage two
In order to solve the above problem, there is a cache, which stores some main memory data that CPU often uses. The speed of cache is almost the same as CPU. When CPU looks for data, it first looks up from cache, and then looks up from main memory. When writing data, write the cached data first, and then update it to main memory. Such a mechanism makes the speed much higher.
stage three
Technology continues to evolve, and on the basis of the above cache, there is a first level, a second level, and a third level cache. The lookup is also layer by layer. If the first level cache does not go to the second level, so on. At this time CPU also got fast read development, from the previous one core into a multi-core CPU (a CPU into multiple parts).
At this time, before only one thread can be run at the same time, now you can run many threads. And from above we can see that each core has a corresponding cache, but the main memory is still that one. Since you can run multiple threads at the same time, then the speed must be on the bar, do not run do not know, a run scared, immediately appeared a lot of problems.
Problem 1: Cache Consistency
That is, each core has its own cache, but the data stored in these caches is different. One picture explains:
Problem 2: Processor optimization and instruction rearrangement
The meaning of this problem is that since the CPU has so many cores, it must be to make full use of resources, so we split the program we wrote and process some code out of order, which is processor optimization. Moreover, the Java virtual machine saw that this operation of the CPU was really strong, so it imitated it and created a just-in-time compiler (JIT), which also did instruction rearrangement operations. Obviously, our code order is scrambled, our instructions are rearranged, and we may not be able to execute as we wish.
These problems appear above, seem to be analyzed from the hardware point of view,"in-depth understanding of java virtual machine" a book then leads to software problems, that is to say, if these problems are converted to the software level will bring what problems?
Question 3: Software issues
(1) Atomic problems
First of all cache coherency problems cause atomicity problems in programs. What does atomicity mean? You have to understand atoms first. Atoms in living things are called indivisible matter. In software, atoms are called indivisible program operations. The atomicity problem must have broken this rule, that is, it was split again in this operation.
In the cache coherency problem, the data of a in two CPU cores is inconsistent, that is, the value of a read from main memory by two CPU cores is different. Then the operation of changing A is definitely not atomic. In the process of changing A, two CPU cores have been changed at the same time.
(2) Visibility issues
When introducing atomicity above, it was said that when two threads (CPU cores) access the same variable, thread 2 modifies the value of the variable, but thread 1 does not see its modification, so it still reads the old data.
(3) Order
That is, the program does not execute in the specified order.
Visibility problems and ordering problems are caused by processor optimization and execution rearrangement.
stage four
In response to so many problems, java virtual machine proposed a java memory model. Effectively solve the three problems that appear above:
Java Memory Model 1.
To contrast with the beginning, we give a diagram of his memory model:
From this diagram we can see 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 a shared variable. All thread operations on variables must be performed in this copy memory, and cannot read or write directly to main memory.
Rule 3: Different threads cannot directly access variables in each other's working memory, and the transfer of variables between threads requires data synchronization between their own working memory and main memory.
Look at the text a bit messy, let's give an example, this example I think is not appropriate, but combined with the above concept, I believe you will understand. In ancient times, droughts often occurred. The imperial court went to disaster relief, so he took a large iron pot to cook porridge. This large iron pot was the main memory, and the porridge inside was data. Each refugee represents a different thread.
(1) Refugees have a bowl with a bowl of porridge. It's like every thread has a local memory and a copy of main memory.
(2) Refugees can only drink porridge in their own bowls and cannot climb directly into the pot to drink porridge, just like threads can only operate data in their own local memory and cannot read and write data directly to the main memory.
(3) One of the refugees wants to share the porridge with his partner. What should he do? The refugee must first pour the porridge into the pot, and then his companion will go to the pot to fill it. Just as threads can't access each other's memory directly, data transfers between them are through main memory.
This example I hope you understand. Now that this model is out, there is still a problem that has not been solved, and that is, what does java provide to implement these three rules?
Because there are so many mechanisms available, we can simply cite a few, such as the synchronized keyword to ensure atomicity and the volatile keyword to ensure visibility. The synchronized keyword and the volatile keyword ensure order. Of course, there are many Lock mechanisms, concurrent packages, etc. are proposed to solve these three problems.
2. The happens-before principle
One of the most important rules is called the happens-before principle, which is proposed to solve visibility. What does that mean?
If the results of one operation need to be visible to another operation, there must be a happens-before relationship between the two operations. For example:
Program order rules: Each operation in a thread occurs before the next, which is happens-before.
(2) Lock rule: For the lock mechanism, it must be locked before it can be unlocked, which is also happens-before.
Volatile field rule: Write operations to a volatile field must occur before read operations.
"Java memory model and Java memory structure what is the difference" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.