package jvm;

import org.openjdk.jol.info.ClassLayout;

public class ShowObjectHeader {

    static final Object lock = new Object();

    public void syncObject() {
        synchronized (lock) {
            try {
                Thread.sleep(1000);
                System.out.println("enter sync");
                System.out.println(ClassLayout.parseInstance(lock).toPrintable());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ShowObjectHeader showObjectHeader = new ShowObjectHeader();
        System.out.println("newly created");
        System.out.println(ClassLayout.parseInstance(lock).toPrintable());

        Thread t1 = new Thread(showObjectHeader::syncObject);
        t1.start();
        t1.join();

        Thread t2 = new Thread(showObjectHeader::syncObject);
        Thread t3 = new Thread(showObjectHeader::syncObject);

        t2.start();
        t3.start();

        t2.join();
        t3.join();
        System.out.println("after sync");
        System.out.println(ClassLayout.parseInstance(lock).toPrintable());

    }
}
