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 second article focuses on the analysis of biased locking delay strategies:
- What is delayed bias
- Why is delayed bias needed
- What is the mechanism of delayed bias
- Effect of delay bias on lock expansion and its proof
- Prove from Hotspot source point of view
It’s a little bit too much, two posts.
What is the
What is biased delay? The bias lock, even if opened, is not immediately usable. There is a delay. Corresponding is BiasedLockingStartupDelay JVM parameter, the default is four seconds, can pass – XX: BiasedLockingStartupDelay modification
why
When you look at bias delay in the JVM, do you have a few questions in your mind: 1. Why bias delay? 2. When does this delay start to be calculated? From JVM startup?
Why design
Answer the first question first. The answer to this question has many versions on the Internet, and the most authoritative answer is this note. Translation: this is a startup time regression solution. By doing this in human language, the JVM can start faster.
Why is it faster? According to the comment: because the JVM takes a number of safety points during startup to eliminate bias. What does this have to do with biased locking? As I understand it, not necessarily what the JVM engineers designed it for. Safety points you should be familiar with, enabling safety points will bring STW. The resetting of bias lock and re-bias judgment also need to enable the safety point, because the virtual machine stack of all threads needs to be scanned, and the memory needs to be static to ensure accurate results. Locks used by the JVM during startup, including those used during the initialization of many classes, go through bias locking logic, and without bias delay, more STW is introduced, resulting in JVM startup times that are too long.
A bit more confusing: Enabling the safe point elimination bias during startup is a first-come-first-happen policy to ensure the order of business for multiple threads interacting during startup. This is similar to the idea of inserting a memory barrier that triggers instant write-back memory in pursuit of low-latency data synchronization.
When to calculate the delay
Take a look at the code above and create a task if there is a delay. The bias delay is performed by the WatcherThread in this task. After the bias delay, the WatcherThread executes the task method, creates a VM_Operation and throws it into the VMThread task pool queue, and waits for the VMThread to execute the task. If there is no delay, it is straightforward to create a task pool queue for VMThread to execute.
By the way, this delay implementation is pretty complicated. In short, Hotspot source code, there are no simple things. As an outsider, I really don’t understand why it’s so complicated. The whole mechanism of biased locking, for example, is also complex.
So where does the delay start? The WatcherThread executes until the sleep method, because the remaining time must be compared to the current time.
Void WatcherThread: : run () {... // Calculate how long it'll be until the next PeriodicTask work // should be done, and sleep that amount of time. int time_waited = sleep(); ... PeriodicTask::real_time_tick(time_waited); }Copy the code
The lock type
Synchronized corresponds to the following lock types:
- unlocked
- Biased locking
- Lightweight lock
- Heavyweight lock
Where are these locks stored? Object header. The structure of each Java object in the JVM is shown below. The Mark Word area is the object header. This is what it looks like when you expand it out
Effect on lock expansion
Good answers start with good questions. Let’s start with these questions:
- What lock was created on the object before the delay bias
- What lock is the object created after deferred bias
- How the lock of the created object is affected by delay bias. I’ll do that in the next video
- After delay bias, will locks held by previously created objects be batch modified
- How does the lock expand with or without delay bias
Delay bias the previously created object is in a lock-free state. Details in the next chapter
The object created after deferred bias is an unbiased bias lock. More on that later
Objects created before the delay bias are not modified in batches after the delay bias. The implication is that objects created before the delay bias are unlocked, and objects created after the delay bias are still unlocked.
Before delay bias, there is no lock, but after expansion, it will not experience bias lock, and will directly expand into lightweight lock
The object created after delayed bias is an unbiased bias lock, which will become biased to the current thread after synchronized
The logic of synchronized lock expansion is discussed in detail in the following article.
series
1. How does the JVM perform synchronized modification methods
Recommended reading
1. How, you ask, are those skilled professionals trained? I’ll tell you
2, from hotspot source level analysis of Java polymorphic implementation principle
3, how to find the native method corresponding Hotspot source
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.
Let me show you some of the projects THAT I’ve written, just to prove that I’m not bragging. I don’t like to brag, I also don’t like water class water articles, heart can not accept.