How to view the object header with synchronized locking

Recently I learned how to lock multiple threads in java to ensure thread safety. The synchronized keyword and lock operation explained can ensure thread safety, but I am still confused about how to achieve thread safety in locking. Whether it is in the book or on the blog, the most talked about is that synchronized is to lock the head of the object, and the threads that lock the same object are synchronized and mutually exclusive.

Syntax: (1). Synchronization code block; (2). Synchronization method.

But I don't even know what an object is.

First of all, we need to know what locks does java have?
Biased locks, spin locks, exclusive locks, shared locks, lightweight locks, heavyweight locks, fair locks, and unfair locks.

So many locks, but what are the locks? Lock code block? Lock object?
-Lock the object head.
But what is the object head?
What is the composition of the java object header?
What constitutes a java object?
Where is the java object header stored?
How much memory does the java object open up on the heap?
—Is the memory development fixed? (Not fixed, otherwise there will be no memory leak)

First of all, what attributes does the object have:
1. Object header (fixed)
2. Instance data of java objects
3. Data alignment

Let's first take a look at the layout of the synchronized built-in lock in memory after it is locked.
(1). First, you need to configure the dependent resources of the pom.xml file of the following idea:

<dependency>
            <groupId>org.openjdk.jol</groupId>
            <artifactId>jol-core</artifactId>
            <version>0.9</version>
</dependency>

(2). Now a test class is used to observe the layout of java objects:

public class lock {
    
    
    boolean f=true;
}

import org.openjdk.jol.info.ClassLayout;

public class Test {
    
    
     static lock l=new lock();
    public static void main(String[] args) {
    
    
        lockTest();
    }
    public static void lockTest(){
    
    
        synchronized (l) {
    
    
            System.out.println("jjj");
            System.out.println(l.hashCode());
            //打印16进制的hashCode
            System.out.println(Integer.toHexString(l.hashCode()));
        System.out.println(ClassLayout.parseInstance(l).toPrintable());
        }

    }
}

(3). The running result shows:
Insert picture description here
So what is the java object header?
-Is the first part of the object, the common part of all objects.
What is the composition of the java object header?
—The object header on the official openjdk document consists of two parts:
1.mark word:
stores the runtime data of the object itself, such as hash code (HashCode), GC generation age, lock status flags, locks held by threads, and biased threads ID, biased timestamp, etc. The length of this part of the data is 32 and 64 Bits respectively in 32-bit and 64-bit virtual machines (not considering the scenario where the compressed pointer is turned on).
2.clazz pointer/ Class Metadata Address

The 01 bits in the screenshot above are divided according to the number of bits, representing the layout, type, GC status, synchronization status of the heap object, and basic information representing the hashCode.

After the synchronized keyword is locked, how to use those 0 and 1 bits to modify the lock state of the object, I may not be able to learn it, I still give up.

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/115128421