Hello, everyone. I’m Mr. Doug Tooth.
I am writing a column about synchronized from the perspective of Hotspot source code. Before and after about 10, will be sent to the whole network, after finishing finishing sorted into e-books put public number for everyone to download. For those interested in this column who want to learn synchronized from the bottom of their minds, follow this wave. E-books will be sorted out through the public number to inform everyone. My public number: hard nuclear teeth.
There are plenty of literature out there on synchronized, so how does this column differ from that?
-
More system. Although data is numerous on market at present, but it is scattered. Some information is even contradictory, so I don’t know whom to believe. I intend to analyze the essence behind each phenomenon after synchronized from the Java level to the JVM level to the operating system level. Synchronized a lot of knowledge is not available on the market, I give it up.
-
Closer to the truth. A lot of data on the market, some are based on the bytecode interpreter that piece of code yy, some are pieced together, each said as true, confusing people. I’m going to start with template interpreter code, step by step debugging research, some unsure to write their own code to prove, strive to share with everyone is the original knowledge. I’ll mark where I’m not sure.
-
It is better to teach a man to fish than to give him fish. I’ll design this column based on the criteria that you can write synchronized by hand. Because in my own research, regardless of the language barrier, if you’re confused about every mechanism of synchronized, you still don’t really understand it. The implication is that you don’t have to write it by hand, but you can think about it in your head, like CAS, lock expansion, lock object unlocking… You kind of know how the code works.
This article is the first to focus on how JVMS perform synchronized modification methods:
- How does the compiler handle the synchronized keyword
- How does the JVM select locked objects
- What does the template interpreter do to improve efficiency
- When does an execution routine cut into C++ code
- How to step through synchronized
Method entry point
JVM execution of Java methods requires that the runtime environment be built before the bytecode instructions are executed.
The runtime environment includes creating stack frames, copying arguments from the caller stack, assigning a value to the this pointer… Synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized synchronized
Because each method call requires building the runtime environment and doing all of these things, the JVM packages the process into a process of execution, one by one. In JVM terminology, the processing logic for general bytecode instructions is called an execution routine, which has been renamed entry Point, which translates to entry point.
There are many entry points in the JVM, which are stored in entry Tables. The more common ones associated with method calls are these four. Non-native methods generally correspond to the first two entry points, and those modified by synchronized correspond to Zerolocals_synchronized, otherwise, zerolocals.
When were these entry points generated? When the JVM starts, look at the code
So when does every method in Java bind to these entry points? In the link phase. Look at the code
You don’t see how entry points for synchronized modified methods are bound. This has to follow the entry_for_method code logic
How to perform
How does the JVM perform synchronized modification methods? This has to compile stage, link stage, run stage three stages to analyze. Any syntactic sugar in any language is done by the compilation system in conjunction with the running system. Of these three phases, the compile phase is what the compile system does, and the link phase and the run phase are what the run system does. So let’s expand on that.
Build system
How does the JVM know at runtime that the method I’m about to execute is not modified by synchronized? Is identified by the access permission of the method.
This data is generated at compile time and can be viewed through the IDEA plug-in Jclasslib.
Operating system
If it is a synchronized block, the method is treated as a normal method during the linking phase, the bound execution stream is Zerolocals, and the synchronized logic is finally processed during monitorenter.
If synchronized modifiers, bind the execution stream zerolocals_synchronized in the link phase. What’s the difference between the two? In fact, the generated flow is the same set of code, the difference is that there is a judgment, if it is synchronized modified method, will execute lock_method. Look at the code.
The logic of the lock_method method is written in assembly style, which is not very easy to understand, so I will use pseudocode to explain
This completes the logic of how the JVM executes synchronized modified methods. Of course, there is much more to Synchronized, which I will share with you step by step. Interested partners can follow a wave. My public number: hard nuclear teeth.
Recommended reading
1, # Java thread creation process details
2. How does JMM understand it? Is JMM related to MESI?
STW, who has been bothering you for most of your life, can finally graduate today
conclusion
In fact, the technology of this industry is really not difficult, if someone takes, foundation 1-2 years, precipitation 2-3 years, is enough. It took me about seven years to figure it out.
I want to show you some of the projects THAT I wrote earlier