“This is the seventh day of my participation in the First Challenge 2022.
preface
- We learned about the Java object memory distribution model.
- Also learn about lock information when stored in
markword
Header information. Previously, we also analyzed the storage situation of Markword through several cases. The caveat here is that you don’t need to use the Lombok plug-in to see HashCode. - Java proposes the concept of locking for concurrent scenarios, and we can use native locking
synchronized
You can also useLock
Related to the lock. It is also used in conjunction with volatile to implement communication between threads. About the use of the lock we will discuss in detail in the next chapter, today we will understand the internal lock about the lock state or different time of the different state lock and its switching process - Synchronized was heavyweight prior to JDK1.6, and since 1.6 the JDK has redefined internal locking as biased, lightweight, and heavyweight
Three performance analysis
- Don’t worry why don’t you start by telling us what the three locks are? Because I think we should first understand the differences between the three types of locks. Why do JVMS upgrade and degrade locks
- Firstly, the computer is divided into user mode and kernel mode. Kernel state can be understood as the computer family, user state is understood as a distant relative
- Also need to operate the computer hardware (money), certain kernel mode (family) will have higher control, and user mode (relatives) can only have a very low control.
- Computer user mode, kernel mode switch to complete the user mode has no right to operate things. For example, if a distant relative wants to borrow money from you, he or she must explain the situation to you. In this case, you are emotional. For distant relatives to get your side, and to explain to you, and to write documents, and to soft and hard to achieve the purpose. This series takes a heavy toll on computer performance.
- The bottom line of synchronized is operating computer hardware. This operation cannot be performed in user mode and involves switching from user mode to kernel mode. This process is called heavyweight locking. In the computer world, switching from user mode to kernel mode may take more than enough time to perform business. So if you add
synchronized
In 70% of scenarios where there is no concurrency but the machine still switches to kernel mode every time, the total time may be twice the overhead. - Based on this scenario, the JVM proposed biased locking, lightweight locking, and heavyweight locking
Biased locking
- Biased locks are provided by the JVM in scenarios where no concurrency is believed to have occurred. Because Oracle research found that plus
synchronized
The keyword indicates that the user wants concurrent locking. But in real scenarios concurrency is not always present, and it may not happen 70% of the time. Even Taobao is just double 11 pressure is too big, there will not be double 11 concurrent magnitude in other time periods. So add thesynchronized
There is no need for the JVM to add a heavyweight lock if the keyword is not competing. So when the JVM detects that no concurrency is currently occurring,synchronized
Inside is a bias lock. - Biased locking is when the JVM writes the current thread to markword through CAS without contention, and successful writing indicates that the lock is successful. In other words cas is bound to succeed because there is no competition. How to determine that there are no race conditions is what the JVM does.
- In the memory distribution section we also list the biased lock markword is the third for
101
。 - In the memory distribution, we just list the identification of each lock. Careful you should notice that there is no concurrency when creating an object in the main thread and locking it. It seems that the last three digits of the markword analyzed by JOL are not
101
- Doesn’t the user here fit the scenario of biased locking? Why is the markword 000? According to the memory distribution section, we found that 000 means no lock state.
- The JVM assumes that there will be no concurrency at startup, and the JVM starts to upgrade biased locks on locked normal objects after the default setting of 4s for performance reasons. Related parameters
-XX:BiasedLockingStartupDelay
- We can get through
java -XX:+PrintFlagsFinal | grep BiasedLock
To view the relevant configuration
- We can see
BiasedLockingStartupDelay=4000
So that’s 4S. So we can either change this parameter to 0 for this setting or the program will print for 5 seconds.
- Wait 5 seconds to appear
101
That is, biased locking. This is where we end up with the concept of favoring locks - In fact, if subdivided, biased lock can be divided into anonymous biased lock and biased lock two concepts. The VM configuration is as follows
-XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0