Hello, everyone. I’m the teacher who gave the people the nickname doug Tooth.
I haven’t updated this article in a long time. I am not lazy ha, is in the preparation of handwriting JVM small class ii class, finally finished. Compared with the first phase, the second phase added a lot of explosive content, so it took a long time to prepare the lesson. What bombshells have been added? On the basis of handwriting JVM, handwriting multithreading mechanism and JMM, handwriting synchronized, handwriting volatile… It will also step through the operating system kernel to reveal any doubts you have about threads. If the core purpose of the first phase is to let you master Pointers and C language, C++ development project thinking, the core purpose of the second phase is to let you master multithreading. At the end of the specific article to introduce you.
Today I will share with you an article for understanding Java multithreading, a particularly important knowledge point: JMM.
The first step in adding threading to the JVM is implementing the JMM, or Java memory model. The JMM is also the foundation for a true understanding of Java multithreading. However, you can say that most of your friends are wrong about JMM. In this article, I will talk about JMM from these aspects, so that you can get a correct and profound understanding of JMM:
- What do you see on the market about JMM
- I will combine a Linux multithreaded program to give you analysis, JMM and MESI have nothing to do with
- So why the JMM? Can’t you?
- How do you understand the visibility impact of volatile or non-volatile Java code?
The day after tomorrow is the Dragon Boat Festival three small holiday, I wish you a holiday in advance, travel attention to safety. Where am I going? “Wake up, you have to produce articles and videos for everyone.”
Get the JMM right
For those of you who have heard JMM for the first time, JMM is the Java memory model, not the JVM memory model. Java multithreading is implemented based on the JMM. If you first heard of it through this article, congratulations, you haven’t been poisoned yet. If you want to know more, keep looking back.
MESI is the CPU’s cache consistency protocol. In its place is MOESI. Cpus of different architectures may also use other protocols. This is enough to understand the passage without further elaboration. Interested partners can learn from Baidu by themselves.
If you search JMM on Baidu, you’ll find articles that follow the same pattern: CPU cache architecture, computer memory model, MESI, volatile real-time trigger data consistency… I do not know who is the originator of this set of ideas, a simple JMM so complicated that normal people could not understand it, and were even persuaded to quit. The point is wrong, and it makes it sound like it’s true. How many of you feel the same way? Give me a paw in the comment section.
So how do you understand the JMM correctly? One, do not associate it with any CPU, OS memory model. Data consistency in these areas is independent of the JMM. Imagine a wall with the OS and hardware on one side and the JVM on the other. JMM is all about the JVM and the hand is not long enough to reach over the wall; JMM = working memory + main memory; Volatile has nothing to do with triggering MESI, but lets the JVM know if I want to write back
If (the variable is volatile) {triggers a write back that writes the changed data back to main memory. There is a write delay when other threads get the modified data}else{do not trigger write back}Copy the code
Because the JMM is implemented abstractly by the JVM itself, if a write occurs, the data in working memory and main memory will normally be inconsistent. If I want to tell the JVM that I need it to be consistent, I need to provide a mechanism, and that mechanism is part of what volatile does.
Two terms to explain: Main memory, or shared memory, is equivalent to the method area + heap area in the JVM memory model. Working memory, or private memory, is equivalent to the virtual machine stack in the JVM memory model.
Some friends may say: these are the son tooth teacher your personal knowledge, how do we know is your correct or the net that set? Yes, there’s nothing wrong with it. As those of you who know me well know, I’m going to start giving evidence. Your reputation depends on the evidence, not the crash.
How do you explain this?
If cache consistency between cpus, and cache consistency between CPU and memory, is triggered by volatile, how is visibility between operating system threads triggered? Will we write code to trigger it manually? So let me give you a sense of that in the last program.
There is no trigger mechanism in the code, so let’s guess: is the output 10 before and after thread 1 sleep? On the answer
The answer is that this change is thread visible. Why do OS threads naturally have thread visibility, but the JMM does not? This drawing is not easy to explain, I will record a video and put it in station B. If you don’t, and you want to figure it out, you can check out the QR code below. OS thread visibility requires assembly specification. The video takes a while, but I’ll do it as soon as I can.
I feel like the gods who associate JMM with MESI really don’t know anything about the bottom line. If you write a multithreaded program at the operating system level, MESI needs to be triggered by an application, which is a travesty. It’s like a cell phone without standby.
Why JMM
Also counter proof: don’t JMM ok? Line? Not line? The answer is yes! But without the JMM, the JVM cannot be cross-platform, and threading mechanisms need to be developed for architecture-specific operating systems. And many of today’s virtual stack implementations, such as synchronized lightweight locks, can be a bit more complicated to implement.
If we were to write our own programs for a specific operating system, such as Ubuntu, it would be very easy to implement the JMM without abstracting it and directly based on the operating system’s threading mechanism. But in keeping with the JVM, my hand-written JVM small classes implement threading, abstracting the JMM. Write this again, put yourself in the perspective of the designer to think, for you to read hotspot source, will be more advantageous. Directly based on the thread mechanism of the operating system to achieve, curious partners can write a write, perhaps you can get different cognition!
Can JMM and MESI be related? Let’s leave a comment.
digression
Always hear partners say: multithreading is difficult to learn, multithreading can’t learn, multithreading has no concept…… Why is that? I think it’s probably true: most people start with a high-level language like Java. The high-level language thread is based on the operating system thread mechanism to achieve, and partners have not learned the operating system application layer learning mechanism, more do not understand the operating system kernel level threading mechanism, so learn multithreading has a kind of like will not will feel. Overuse terms such as application state, kernel state, and thread scheduling when communicating with others or in interviews. Writing multithreaded programs is even worse.
The addition to my handwritten JVM class is to make you proficient in multithreading in a way THAT I understand. It can be a little difficult to learn, so if you don’t have the determination to become a tech juggler, come back when you do. I’m always here. Of course, learning at the bottom will be boring, so how do I solve this problem? Handwritten. Immersed in the perspective of the designer to think and realize, with a little understanding of the knowledge point itself, a little bit of code forming, the sense of achievement is unmatched by CRUD. Let me show you the new syllabus for the next issue.
Here we go! Son tooth teacher, the entire network of the only handwritten JVM class ii began to recruit students! Sign up now for early bird rates. This course down, if you really absorb, immediately ahead of the people around several levels. Continue to precipitation, write more works, resume more substantial, the underlying strength is more powerful. Going back to the application layer, you can understand it very quickly, and understand it more deeply than those who don’t understand the bottom layer. More importantly, you can learn hard core content without stepping on my toes. Everyone watching my videos, my articles, should be able to feel that I advocate is hardcore.
If you are tempted, if you realize the importance of the bottom layer, if you want to become a technology leader deep in your heart, welcome to learn with me, add my wechat to find my consultation course (VX: JVM-Ziya). If you want to ask me: what is the use of learning this? I don’t need it for work. I’d say you’re young.