If there is any mistake, welcome criticism and correction
The cause of
In Mr Zhi-ming zhou “the deep understanding of JAVA virtual machine,” chapter to the optimization of the lock, when an object is currently in a state of biased locking, and received the request of the need to compute the hash code consistency, its deflection status will be revoked, immediately and heavyweight locks, lock will expand into the reason is because objects head Mark in the Word, The thread ID address for storing bias locks conflicts with the bits for storing consistency hashes
If the object is a lightweight lock, wouldn’t the Mark Word for a heavyweight lock conflict with the Hash encoding
Let’s do an experiment
Mark Word(HotSpot)
In a 32-bit system, the following data is stored:
64-bit system implementation:
Memory visualization tool
Maven, add the following depend on by ClassLayout. ParseInstance (object). ToPrintable print () method directly in the console object memory
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>
Copy the code
Unlocked state
Start by defining a simple Person class
public class Person { public final String name; public final int age; public Person(String name, int age) { this.name = name; this.age = age; }}Copy the code
Create a simple object to see what happens before and after the memory hash
public class test { public static void main(String[] args) throws UnknownHostException { Person json = new Person("json", 14); System.out.println(ClassLayout.parseInstance(json).toPrintable()); json.hashCode(); System.out.println(ClassLayout.parseInstance(json).toPrintable()); }}Copy the code
>>Integer.toBinaryString(json.hashCode())
>>1101011 10010110 01010001 11110011
Copy the code
As you can see, the hash value exists at 26-56 bits after the size is excluded
The bias lock flag is 0 and the lock flag bit is 01
Biased locking
Public class test {public static void main(String[] args) throws InterruptedException {// Causes the thread to enter a slanting state Thread.sleep(5000); Person json = new Person("json", 14); System.out.println(ClassLayout.parseInstance(json).toPrintable()); Synchronized (json) {/ / biased locking System. Out. The println (ClassLayout. ParseInstance (json) toPrintable ()); json.hashCode(); / / lock escalation System. Out. Println (ClassLayout. ParseInstance (json) toPrintable ()); }}}Copy the code
Lightweight lock
public static void main(String[] args) throws UnknownHostException{ Person json = new Person("json", 14); / / unlocked System. Out. Println (ClassLayout. ParseInstance (json) toPrintable ()); Synchronized (json) {/ / lightweight lock System. Out. Println (ClassLayout. ParseInstance (json) toPrintable ()); json.hashCode(); / / upgrade for the heavyweight lock System. Out. Println (ClassLayout. ParseInstance (json) toPrintable ()); }}Copy the code
At this point, because the partial lock is not available, add the lightweight lock directly, after hash, directly upgrade to the heavyweight lock (happy, confusion solved).
Heavyweight lock
So where does the hash go when you switch to a heavyweight lock?
Here’s a quote from Big R
This is a problem with the lock implementation for HotSpot VM. The short answer is:
- When an object has computed an Identity Hash code, it cannot enter the biased lock state;
- When an object is currently in a biased lock state and needs to calculate its identity Hash code, its biased lock will be removed and the lock will expand to a weight lock.
- In the implementation of weight lock, ObjectMonitor class has a field to record the mark Word in the unlocked state, which can store the value of Identity Hash code. Or simply the weight lock can store the identity hash code.
Please note that the hash codes discussed here are for Identity Hash codes only. The value returned by the user-defined hashCode() method is different from the one discussed here. Identity hash code is not written in the Java lang. Object. The hashCode () or Java. Lang. System. IdentityHashCode (Object) the returned value.
Author: RednaxelaFX links: www.zhihu.com/question/52… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.
Lock upgrade process
Now looking back at the lock upgrade process, it is much clearer